The following statement is wrong:
"As we can see in this example each object has its own copy of class variable."
I guess you mean
As we can see in this example all the object share the class variable "noOfInstances" (which counts the number of instances of class
StaticVariable)
I have been continuously searching for perfect example to understand "static" keyword.finally i found out here...am very much satisfied, the way they explained here..
All instances of the same class share a single copy of the static variable.the static variables are initialise exactly when a class is loaded, i.e all static variable in a class are initialize before any object of that class can be created. we can initialize value to static variable, but if you dont explicitly initialize a static variable, it gets default value i.e 0
if a method is declared as a static than there is no need to create objet.we can directly call that method as following...
classname.methodname();
where classname and methodname is declared as static.
one thing more about static variable is that "non static variable cannot be used inside of a method which is declared as static."
ex:- class room
{
int x,y;
static getdata(int l,int b)
{
x=l; //here x andy is non static member
y=b; //which is used in side static method
} //that is not legal to write.
}
correction jee October 13, 2011 at 3:27 PM
The following statement is wrong: "As we can see in this example each object has its own copy of class variable." I guess you mean As we can see in this example all the object share the class variable "noOfInstances" (which counts the number of instances of class StaticVariable)
best illustration to understand static keywordIyyanarGopal December 12, 2011 at 12:49 PM
I have been continuously searching for perfect example to understand "static" keyword.finally i found out here...am very much satisfied, the way they explained here..
static variables in javapaurnima ingale January 17, 2012 at 12:01 AM
All instances of the same class share a single copy of the static variable.the static variables are initialise exactly when a class is loaded, i.e all static variable in a class are initialize before any object of that class can be created. we can initialize value to static variable, but if you dont explicitly initialize a static variable, it gets default value i.e 0
javakuna simhachalam January 12, 2013 at 5:06 PM
Yes
about static variable/method...ranvijay singh rajput March 30, 2012 at 12:18 PM
if a method is declared as a static than there is no need to create objet.we can directly call that method as following... classname.methodname(); where classname and methodname is declared as static. one thing more about static variable is that "non static variable cannot be used inside of a method which is declared as static." ex:- class room { int x,y; static getdata(int l,int b) { x=l; //here x andy is non static member y=b; //which is used in side static method } //that is not legal to write. }
about static member in javaarchana yadav October 21, 2012 at 11:51 PM
please give the proper definition of static member with example in java.
javaprem jha November 1, 2012 at 5:08 PM
static variable
Needs clarification...Mohsin November 26, 2012 at 3:03 PM
But if we didn't increase the value in constructor then it gives us 0.... repetitively :/
Post your Comment