hi what is meant by static methods and static variable

Recommended Answers

All 5 Replies

These methods and variables don't need an instance of the object to be used:
If in an object: MyObject there is the static variable: VAR and the static method method1(), then instead of:

MyObject my=new MyObject();
String s = my.VAR;
my.method1();

you can also do this:

String s = MyObject.VAR;
MyObject.method1();

One other thing with static variables is that in memory they are common for all instances of the object. When you create different instances of the same object the variables of the object are different and they take their own space in memory, but with static no matter how many instances you create the variable will be ONE.
Also if you create an instance and you change the value of a static variable with it,
and then you create a new instance the static value of the new instance will be the same as the one previously changed. All the different instances have access to ONE variable and they all change the same variable.

Soon an example about the last one

public class TestClass {
    public static int count=0;
    
    /** Creates a new instance of TestClass */
    public TestClass() {
    }
    
    public static void main(String [] args) {
        TestClass tc1=new TestClass();
        tc1.count=5;
        
        TestClass tc2=new TestClass();
        System.out.println(tc2.count); //will print 5 NOT 0
        
        TestClass.count=10;
        
        System.out.println(tc1.count); //will print 10
    }
}

Note that since count is static all instances manipulate the same variable.

Now in a non-static method you can use static methods and variables.

But in static methods, you cannot use non-static methods and variables. Because non-static methods and variables need to be called by an instance of the object and the static method in which they are can be called without an instance of the class.
Of course in order to bypass that you can create a new Object in the static method, depending if this is what you really want.

Like previously stated, the static modifier allows Objects that consist of that static data type to share data from the same address.

One word of warning though - static variables are initialized once and only once! Not only that, but the scope of static is contained within the JVM it was declared in.

Here's an example of a static field being initialized once--

public class StaticTest{

	// static member - initalized to zero once per definition
	static byte timesCreated = 0;

	// static context - happens once per JVM
	static{
		System.out.println("Time static block ran = " + (++timesCreated));
	}

	// static member - initalized to zero once per definition
	static byte tC2 = 0;
	
	// non-static context (each class gets its own one of these)
	{
		System.out.println("Times anonymous block ran = " + (++tC2));
	}

	public static void main(String... args){
		new StaticTest(); // first object of type StaticTest created
		new StaticTest(); // second " "
	}
}

hi what is meant by static methods and static variable

Hi,

static in Java means you can use static variables and methods without instantiation of that class where they are defined means you don't have to create any object of that class to use those static members.

These static members are loaded at run time when the class loads.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.