Java Static Keywords

The static is a keyword defined in the java programming language. Static keyword is used to define class member. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class.

  • variable ( class variable)
  • method ( class method)
  • block
  • nested class

Note: You can specify a member by static keyword, only in the scope of class out side of method or any block.

Java static variable

If you declare any variable as static, it is known static variable.

The static variable can be used to refer the common property of all objects

The static variable gets memory only once in class area at the time of class loading.

Understanding problem without static variable

		
class Student {
	int rollno;
	String name;
	String college = "SMU";
}

Example of static variable

 class Student {
	int rollno;
	String name;
	static String college = "SMU";

	Student(int r, String n) {
		rollno = r;
		name = n;
	}

	void display() {
		System.out.println(rollno + " " + name + " " + college);
	}

	public static void main(String args[]) {
		Student s1 = new Student(111, "Mithilesh");
		Student s2 = new Student(222, "Rahul");
		s1.display();
		s2.display();
	}
}


111 Mithilesh SMU
222 Rahul SMU

Java static property is shared to all objects.


Java static method

If you apply static keyword with any method, it is known as static method.

  • It is used to define the behavior of a class.
  • It simply required a class reference for its invocation/calling.

Example to understand static method


class Calculate {
	static int cube(int x)// static method
	{
		return x * x * x;
	}

	public static void main(String ar[]) {
		int result = Calculate.cube(5);// static method invoking by reference of
										// class
		System.out.println(result);
	}
}

Restrictions for static method

There are two main restrictions for the static method. They are:

  • he static method can not use non static data member or call non-static method directly
  • this and super cannot be used in static context
class A {
	int b = 60;// non static

	public static void main(String args[]) {
		System.out.println(b);
	}
}

Example to understand static data member, block and method in a single program

class Calculate {
	static int num;// static data member
	static// static block
	{
		num = 5;// initializing static data member
	}

	static int cube(int x)// static method
	{
		return x * x * x;
	}

	public static void main(String args[]) {
		int result = Calculate.cube(num);// static method invoking by reference
											// of class
		System.out.println("result using class referece " + result);
		Calculate c = new Calculate();
		int result1 = c.cube(num);// static method invoking by reference of
									// object
		System.out.println("result using object referece " + result1);
	}
}

Why java main method is static?

Because object is not required to call static method if it were non-static method, jvm create object first then call main() method that will lead the problem of extra memory allocation.


Java static block

It is a block of statement that is implicitly execute after a class loaded. It is used to initialize static data members of the class.

  • Is used to initialize the static data member.
  • It is executed before main method at the time of class loading.

Can we execute a class without main method?

Answer : Yes, you can write your code in static block which executes before main method. But you can do it only till JDK 6 in following manner.


 class Simple {
	static {
		System.out.println("executing without main...");
		System.exit(0);// exiting JVM before search of main
	}
}

					

In JDK 7 and above, following error will be occurred for the above program.

Error: Main method not found in class Simple, please define the main method as: public static void main(String ar[])


What are the limitations of static block and static method?

static block and static method has following limitations.

  • Only static data members of the class can be referred within static block or method, i.e. Non-static data member can not be referenced in a static block or method.
  • A static block or method can directly invoke only others static methods of the class. i.e. Non-static method can not be directly invoked from a static block or method.
  • this and super keyword can not be used in a static block or method.

Static keyword interview questions in java
  • What is a static method?
  • Why use of static keyword in java?
  • What is the use of static in java?
  • What do you mean by static in Java?
  • What is the use of static block in Java?
  • What is a static method in java?