permaculture playing cards*
The moose likes Beginning Java and the fly likes static modifier Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static modifier" Watch "static modifier" New topic
Author

static modifier

nirali shah
Greenhorn

Joined: Apr 16, 2009
Posts: 24
Static variable is initialized at the class level only, while non-static variable can only be declared at class level but can not be initialized at class level. If a variable is initialized at class level then it gives error at compile time. Is this correct ?
Can a static variable's value be changed in the class ?
If yes, then that means that variable is like other variable whose value can be changed. So whats the difference between static and non-static variable ?
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

If yes, then that means that variable is like other variable whose value can be changed. So whats the difference between static and non-static variable ?

It looks like you didn't fully understand the concept of static variable. Usual variables (instance members) are owned by an instance of a class, while a static variable is shared by all instances of the same class.
Look at this class with public members (don't do this ):

With the following code, you can see that myInstanceVariable is owned by one and only one instance, and that myStaticVariable is shared by all.


[My Blog]
All roads lead to JavaRanch
Monu Tripathi
Rancher

Joined: Oct 12, 2008
Posts: 1369
    
    1

You can view a static variable as a global variable(enclosing class just provides a namespace) where its values is visible to all the other classes using ClassName.variablename syntax. So, in memory there would be a single variable, common for all(every piece of code that uses this variable).

Instance variables are actual implementations(objects) of the abstraction defined by a class. When you create objects of a class, non-static variables of the class become properties/fields of these objects. Each object will have a separate copy of the non-static variables for itself(Refer to Mr.Christophe Verré's code above).

Also, memory is allocated to static variables when a class is loaded. Memory for non-static variables is allocated only when objects of the class are created.


[List of FAQs] | [Android FAQ] | [Samuh Varta]
Nishant Arora
Greenhorn

Joined: Apr 21, 2009
Posts: 16
/* Think Universe as the class then each one of us have sense to see, sniff, taste these are non-static method.
Now if you do not have eye, nose, tongue i.e any of the sense then you will have emptiness only. This is static If you
can change this emptiness so that other people could observe this change then its the concept of static in oops.

static variable
Do not take whole as one word instead
static - because only one copy.
variable - because can be changed by any object of class.
*/

//This is example how count shared by all object of Object Counter.


public class ObjectCount {

private static int count=0;
public static int getCount(){
return count;
}

public ObjectCount()
{
count++;
}

public static void main(String[] arg)
{

ObjectCount obj[];
obj=new ObjectCount[5];
for(int i=0;i<obj.length;i++)
{

obj[i]=new ObjectCount();

}
System.out.print("Total Count ="+ObjectCount.getCount());
}

}
nirali shah
Greenhorn

Joined: Apr 16, 2009
Posts: 24
thanks.
About static variable, I am clear. It can be used by any instance of a class, but for all the instances its value will remain the same.
I am still confused with static method. How does it return the same value every time ? As a method is supplied with different values as parameters all the time. So a static method will work just like as a non-static method.
Can you please explain this ?
Devaka Cooray
ExamLab Creator
Saloon Keeper

Joined: Jul 29, 2008
Posts: 3013
    
  35

nirali shah wrote:thanks.
I am still confused with static method. How does it return the same value every time ? As a method is supplied with different values as parameters all the time. So a static method will work just like as a non-static method.
Can you please explain this ?


Nirali,

Don't get confused with static methods. You have to understand that static variables and static methods are two different animals. Static methods are not supposed to return the same value every time. They can be used to perform some generic task, without doing an object creation. Because static methods can be accessed directly through the class name, without creating an object of it.



Consider the code above. If we need to access the normalMethod(String), we have to create an object from this MClass, as below:


But we can NOT call it as below:


Think about that, a nonstatic method can access some nonstatic variables. Here we accessed the nonstatic variable 'x' from the normalMethod(). The value of a nonstatic variable always depends on the corresponding OBJECT of that class, (variable 'x' may have different values in several objects of MClass). That is why we always need to create an object of the corresponding class, before we can call to a nonstatic method.

But in static methods, we can NOT access nonstatic variables within a static method. For an example, you can NOT access the variable 'x' within the staticMethod(), as we did in normalMethod(). If you attempt to do so, you will get a compile-time error. The meaning of this is, we can access ONLY static variables (and other static methods), from a static method. You might know that static variables are NOT depend on the object. It should have the same value, even in different objects. Therefore static methods are stand-alone; no need to such details from an object. That is why we CAN access a static method as below, without creating an object:

MClass.normalMethod();

This is the main advantage of using static methods and variables.

Cheers,

Devaka.


Founder of ExamLab and Systemup
See how I can help you to become an awesome programmer
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: static modifier
 
Similar Threads
where do static variables live ?
Difference between Static and Instance variable
What will be the output?Why? - extension
Assignment Problem
final variables