Static variable code demo

public class StaticVariables {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test=new Test();	
		Test test2=new Test();
		Test test3=new Test();
		Test test4=new Test();
	}
}


class Test{
	static int i=0;
	
	Test(){
		i++;
		System.out.println(i);
	}
}

Output:

1
2
3
4

Static variables as demonstrated above are remembered between states, so when a new object is created, the static variable is remembered, so this allows us to give a unique reference number to objects of the same type.

This can be demo’d below by adding a new class ‘horses’

public class StaticVariables {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test=new Test();	
		Test test2=new Test();
		Test test3=new Test();
		Test test4=new Test();
		
		Horses horse1= new Horses();
		Horses horse2= new Horses();
	}
}


class Test{
	static int i=0;
	
	Test(){
		i++;
		System.out.println(i);
	}
}

class Horses{
	static int i=0;
	
	Horses(){
		i++;
		System.out.println(i);
	}
}

Output:

1
2
3
4
1
2

Now we can see that even though we’re using the same static variable i in class horses, this is NOT added onto the Test class static variable, as it’s a different class.

In Horses it is outside the scope of the Test class, so begins uniquely counting the horses objects.

Static variables are therefore a common variable shared between objects of the same type.

Take a look at the following code:

public class StaticVariables {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Test test=new Test();	
		Test test2=new Test();
		Test test3=new Test();
		Test test4=new Test();
		
		Horses horse1= new Horses();
		Horses horse2= new Horses();
		
		System.out.println(Test.i);
		
		System.out.println(Horses.i);
	}
}


class Test{
	static int i=0;
	
	Test(){
		i++;
	}
}

class Horses{
	static int i=0;
	
	Horses(){
		i++;
	}
}

Outputs:

4
2

Here we can see the since the static variable is effectively separate from the individual objects, we can access the static variable by saying

[Class].[Static Variable]

e.g.

Test.i

or

Horses.i

So;

  • Static variables belong to the class
  • They are accessed using [Classname].[StaticVariableName] outside the class
  • Only one copy of the static variable is maintained across all the objects
  • Every method of the class can access the static variable including constructors
  • Class wide information common to every object should be static
  • Static variable initialisation 1

    public class StaticVariables {
    
    	public static void main(String[] args) {
    
    		System.out.println(Horses.count);
    		System.out.println(Horses.name);
    	}
    }
    
    
    
    class Horses{
    	
    	static int count;
    	static String name;
    }
    

    Outputs:

    0
    null

    Notice how we DO NOT NEED to create an object to access the static variables, as they belong to the class, not the object (that we create from them)

    If static variables are not initialised with values; we get defaults:

    0 for numbers
    false for boolean
    ‘0’ for characters
    null for objects

    Static variable initialisation 2

    public class StaticVariables {
    
    	public static void main(String[] args) {
    
    		System.out.println(Horses.count);
    		System.out.println(Horses.onOff);
    		System.out.println(Horses.name);
    		System.out.println(Horses.a);
    	}
    }
    
    
    class Horses{
    	
    	static int count =8;
    	static boolean onOff = true;
    	static char name='J';
    	static Object a=new Object();
    }
    

    Outputs:

    8
    true
    J
    java.lang.Object@1aa8c488

    As you can see, we can initialise them just like any other normal variables. We can give explicit values directly or inside constructors.

    Static variable initialisation : The static block

    public class StaticVariables {
    
    	public static void main(String[] args) {
    
    		Horses horse = new Horses();	
    	}
    }
    
    
    class Horses{
    	
    	static int count;
    	// static initialisation block
    	static 
    	{
    		count = 105;
    		System.out.println("First");
    	}
    	
    	Horses()
    	{
    		System.out.println("Second");
    	}
    }
    

    Output:

    First
    Second

    The contents of the static block (shown above) executes before any object is created.

    Static variable initialisation : Multiple static blocks

    public class StaticVariables {
    
    	public static void main(String[] args) {
    
    		Horses horse = new Horses();	
    	}
    }
    
    
    class Horses{
    	
    	static int count;
    	// static block
    	static 
    	{
    		count = 105;
    		System.out.println("First");
    	}
    	
    	static 
    	{
    		count = 288;
    		System.out.println("Second");
    	}
    	
    	Horses()
    	{
    		System.out.println("Horses constructor");
    	}
    }
    

    Output:

    First
    Second
    Horses constructor

    The static blocks executed in the order that they were written BEFORE the constructor when “Horses horse = new Horses();” is executed.

    Leave a Reply