Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials

>Home>Java Tutorial>The Java Static Variables with Examples

Java Tutorial

Basic Concepts

Object Oriented Concepts

Coming Tutorials



The Static Variables in Java Examples

If the value of a variable does not vary from object to object then it is never recommended to declare that variable at object level, we should declare such type of variables as class level by using static modifiers.

The static variables get one time memory allocation and maintain a single copy for a class which can be shared by all the objects of the class. Therefore the static variables make program more memory efficient.

In the case of instance variables for every object a separate copy will be created but in the case of static variable single copy will be created for a class.

Static variables will be created at the time of class loading and destroyed at the time of class unloading. Hence the scope of the static variable is exactly same as the scope of the class.


Rules of static variable in Java

  • The static variable maintains single copy for a whole class.

  • The static variables cannot be declared within a method or block.

  • If the value of a static variable changed by an object then it gets reflected into all the objects of the class.

  • The static variables get created at the time of class loading and destroy at the time of class unloading.

  • A static variable can be accessed using a reference variable or class name.

  • A static variable can be used to hold a value which is common for all the objects of a class.

  • The JVM assigns default values to the static variables.


Syntax:

static datatype variableName;



Example 1: The static variables can be accessed from static and instance methods directly.


class Sample
{
static int x = 10;// static variable

 // main() method
public static void main(String[] args)
{
// can access static variables from static method directly
System.out.println(x);// 10

// create an object
Sample s1 = new Sample();

// call a instance method m1()
s1.m1();
}
 public void m1()
{
//access static variable x from instance method m1() directly
System.out.println(x); // 10
}
}  

Example 2: For the static variables it is not required to perform initialization explicitly, the JVM will provide default values at the time of class loading.


class Test
{
// static variables declaration
static int i;
static boolean b;
static String s;

 // main() method
public static void main(String[] args)
{
// print values of instance variables
System.out.println(i);// 0
System.out.println(b);// false
System.out.println(s);// null
}
}

Example 3: If we change value of an instance variable then it will not get reflected in other objects, because every object has a separate independent copy of instance variables. But if we change the value of a static variable then it will gets reflected in all the objects because a single copy of a static variable is maintained for a class which is shared by all the objects of the class.


class Student
{
int rollNo;
String name;
static String collegeName;//static variable
}

class StaticVar { public static void main(String[] args) { //create 3 object which will share collegeName value Student s1= new Student(); Student s2= new Student(); Student s3= new Student(); //assign value to static variable collegeName Student.collegeName="Topper Skills"; //assign values to instance variables s1.rollNo=1; s1.name="stud1"; s2.rollNo=2; s2.name="stud2"; s3.rollNo=3; s3.name="stud3"; //Print the values of the objects System.out.println("S1 Roll No.= "+s1.rollNo+" S1 Name= "+s1.name+" S1 College Name= "+s1.collegeName ); System.out.println("S2 Roll No.= "+s2.rollNo+" S2 Name= "+s2.name+" S2 College Name= "+s2.collegeName ); System.out.println("S3 Roll No.= "+s3.rollNo+" S3 Name= "+s3.name+" S3 College Name= "+s3.collegeName ); //if one object change the value of static variable then it will reflect into all objects s2.collegeName="my college"; s2.name="Devavrat"; //Print the values of the objects after change System.out.println("S1 Roll No.= "+s1.rollNo+" S1 Name= "+s1.name+" S1 College Name= "+s1.collegeName ); System.out.println("S2 Roll No.= "+s2.rollNo+" S2 Name= "+s2.name+" S2 College Name= "+s2.collegeName ); System.out.println("S3 Roll No.= "+s3.rollNo+" S3 Name= "+s3.name+" S3 College Name= "+s3.collegeName ); } }

Example 2: Use of static variable as a common counter.



class StaticVarDemo
{
int noOfObject;// instance variable as counter
static int noOfObjectStat;// static variable as counter
StaticVarDemo()
{
// increment the counters whenever objects get created
noOfObject++;
noOfObjectStat++;
}

public static void main(String[] args)
{
// create 4 objects, the constructor will be called 4 times
StaticVarDemo obj1=  new StaticVarDemo();
StaticVarDemo obj2=  new StaticVarDemo();
StaticVarDemo obj3=  new StaticVarDemo();
StaticVarDemo obj4=  new StaticVarDemo();
System.out.println("Number of objects by instance counter are "+obj4.noOfObject);// 1
System.out.println("Number of objects by static counter are "+obj4.noOfObjectStat);// 4
  }
}

Share the article to help your friends