Static in java

To Do

  • Where is static data stored in java memory?
  • Static local variable in java
  • can a inner class be static?
  • can a inner class be final?
  • Object of a inner class or an object of inner static class be static?

static keyword can be applied to :

  • Block
  • Variable
  • Method
  • Nested Classes

Static Block :

  • Used to initialize static variables when you need to do some computation before it.
  • One of the places where static final variables can be assigned.
  • Kind of constructor for static data members in the class.
  • When is static block called
    • Static block is called only once.
    • When is the static block called? when the first object is created of that class or when the static method of a class is called
    • Order of execution of static block ? Before a constructor is called the static block is called, the order is i.e Static Block, Instance Initializer Block, Constructor.
    • When multiple static block ? Then the order of execution of static block will be in the order in which the written into the code.
    • If there are multiple static variables and multiple static block inbetween each other then the order of execution will also be in the order in which it is written.
      Example :
      static int x=f1();
      static {
      static int y = f3();
      Function f1(), f2(), f3() and f4() will be executed in orderly fashion.

Static Variable :

  • When a variable is declared as static, then a single copy of variable is created and shared among all objects at class level.
  • We can create static variable at class level only, no local variable can be static.
  • Static block and static variables are executed in order they are present in a program.
  • Static variable can be access by object as well as into methods

Static Method :

  • One static method can call another static method
  • Static method can only access static variables of that class and the local variables inside that method.
  • If you try to access an instance variable inside a static method we get compilation error “Cannot make a static reference to the non-static field <variable_name>”
  • We can also access static methods using objects but we get a compilation warning saying that the method <method_name> of class <class_name> should be accessed in a static way.
  • You cannot use this or super keyword in a static method.

Static Nested Class :

Static nested classes are not technically an inner class. They are like a static member of outer class.

Static inner class in the start looks very confusing but when you the basic idea you will under stand it in a better way :
a. These class cannot access non static data members and methods of the outer class.
b. You can create object of the static inner class without the object of outer class
c. You can have static and non static methods and variables inside static inner class just like any other inner class
d. To access the static data members of static inner class you need not have object of outer or static inner class.
e. To access the non static data members you need not have object out the outer class but need an object of static inner class.
f. Perfect example of Static inner class is Builder Design Pattern

public class StaticInnerClass {
	public static void main(String[] args) {
		//Point (b)
		Outer.Inner obj = new Outer.Inner(); //No Outer() is added since it is static
		//Point (d)
		Outer.Inner.printStatic();//Can also be written as Inner.printStatic();
		//Point (e)
class Outer{
	int x;
	static int y;
	protected static class Inner{
		// Point (c) non static
		void print() {
			//Point (a)
			//System.out.println("instance method"+x); compilation error
			System.out.println("instance method"+y);
		// Point (c) static
		static void printStatic() {
			System.out.println("Static method");

Leave a Comment