Home Java Beginners Example of a class variable (static variable)

 
 

Share on Google+Share on Google+

Example of a class variable (static variable)

Advertisement
This Java programming example will teach you how you can define the static class variable in a class. When a number of objects are created from the same class, each instance has its own copy of class variables. But this is not the case when it is declared

Example of a class variable (static variable)

     

This Java programming example will teach you how you can define the static class variable in a class. When a number of objects are created from the same class, each instance has its own copy of class variables. But this is not the case when it is declared as static static.

static  method or a variable is not attached to a particular object, but rather to the class as a whole. They are allocated when the class is loaded. Remember, each time you call the instance the new value of the variable is provided to you. For example in the class StaticVariable each instance has different copy of a class variable. It will be updated each time the instance has been called. We can call class variable directly inside the main method.

To see the use of a static variable first of all create a class StaticVariable. Define one static variable in the class. Now make a  constructor  in which you will increment the value of the static variable. Now make a object of StaticVariable class and call the static variable of the class. In the same way now make a second object of the class and again repeats the process. Each time you call the static variable you will get a new value.

Code of this example is given below:

public class StaticVariable{
  static int noOfInstances;
  StaticVariable(){
  noOfInstances++;
  }
public static void main(String[] args){
 StaticVariable sv1 = new StaticVariable();
 System.out.println("No. of instances for sv1 : " + sv1.noOfInstances);

 StaticVariable sv2 = new StaticVariable();
 System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);
 System.out.println("No. of instances for st2 : "  + sv2.noOfInstances);

 StaticVariable sv3 = new StaticVariable();
 System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);
 System.out.println("No. of instances for sv2 : "  + sv2.noOfInstances);
 System.out.println("No. of instances for sv3 : "  + sv3.noOfInstances);
 }
}

Output of the program is given below:

As we can see in this example each object has its own copy of class variable.

C:\java>java StaticVariable
No. of instances for sv1 : 1
No. of instances for sv1 : 2
No. of instances for st2 :  2
No. of instances for sv1 : 3
No. of instances for sv2 : 3
No. of instances for sv3 : 3

Download this example:

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: March 11, 2008

Related Tutorials

Discuss: Example of a class variable (static variable)   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:9
akash
August 18, 2011
Java

can I call static variable with object also. or just class name is allowed?
jee
October 13, 2011
correction

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)
IyyanarGopal
December 12, 2011
best illustration to understand static keyword

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..
paurnima ingale
January 17, 2012
static variables in java

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
kuna simhachalam
January 12, 2013
java

Yes
ranvijay singh rajput
March 30, 2012
about static variable/method...

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. }
archana yadav
October 21, 2012
about static member in java

please give the proper definition of static member with example in java.
prem jha
November 1, 2012
java

static variable
Mohsin
November 26, 2012
Needs clarification...

But if we didn't increase the value in constructor then it gives us 0.... repetitively :/
DMCA.com