Scroll to top

Java Static Keyword

What is Static

Static is a Non Access Modifier.

Applicable to

The Static keyword can be applied to

  • Method
  • Variable
  • Class nested within another Class
  • Initialization Block

Not Applicable to

The Static keyword cannot be applied to

  • Class (Not Nested)
  • Constructor
  • Interfaces
  • Method Local Inner Class(Difference then  nested class)
  • Inner Class methods
  • Instance Variables
  • Local Variables

 

Purpose of Static Keyword in Java

The static word can be used to attach a Variable or Method to a Class. The variable or Method that are marked static belongs to the Class rather than to any particular instance. It is opposite to Instance variable.

How to Invoke

Static variable and Methods can be used without having an instance of the Class. Only the Class is required to invoke a static Method or static variable.

/*
 * Here we will learn to access Static method and Static Variable.
 */
public class JavaStaticExample {

	static int i = 10;

	static void method() {
		System.out.println("Inside Static method");
	}

	public static void main(String[] args) {

		// Accessing Static method
		JavaStaticExample.method();

		// Accessing Static Variable
		System.out.println(JavaStaticExample.i);

		/*
		 * No Instance is required to access Static Variable or Method as we
		 * have seen above. Still we can access the same static variable and
		 * static method using Instace references as below.
		 */
		JavaStaticExample obj1 = new JavaStaticExample();
		JavaStaticExample obj2 = new JavaStaticExample();

		/*
		 * Accessing static variable in Non Static way. Compiler will warn you
		 * with below warning.
		 *
		 * The static field JavaStaticExample.i should be accessed in a static
		 * way.
		 */
		System.out.println(obj1.i);
		// Accessing satic method using reference.
		// Warning by compiler
		// "The static method method() from the type JavaStaticExample should be accessed in a static way"
		obj1.method();
	}
}

Output of the above program

Inside Static method
10
10
Inside Static method

Note*: Static keywords can be used with variables and Methods. It is not applicable to Class.

 

Class Variables – Static Fields

Class variables also known as static fields share characteristics across all Objects within a Class. When you declare a field to be static, only a single instance of the associated variable is created, which is common to all the Objects of that Class. Hence when one Object changes the value of a Class variable, it affects all the Objects of the Class. We can access a Class variable by using the name of the Class, and not necessarily using a reference to an individual Object within the Class. Static variables can be accessed even when no Objects of that Class exists. Class variables are declared using the static keyword.

 

Class Methods – Static Methods

Class Methods, similar to Class variables can be invoked without having an instance of the Class. Class methods are often used to provide global functions for Java programs. For example, Methods in the java.lang.Math package are Class methods. You cannot call non-static Methods from inside a static Method.

Static Keyword Rules

  • Variable or Methods  marked static belong to the Class rather then to any particular Instance. 
  • Static Method or variable can be used without creating or referencing an instance of the Class. 
  • If there are instances, a static variable of a Class will be shared by all instances of that class, This will result in only one copy.
  • A static Method can’t access a non static variable nor can directly invoke non static Method (It can invoke or access Method or variable via instances).

 

Cheat-sheet

  • Static is a Non-Access Modifier.
  • The Static modifier can be applied to a variable or Method or block or inner Class.
  • Static members belong to Class only not an instance.
  • A Static Method cannot access an instance variable.
  • Static Methods cannot be overriden as they are Class specific and don’t belong to an Instance.
  • Static Methods can be redefined.
  • If a Class contains any static blocks, then that block will be executed only when the Class is loaded in JVM. Creating multiple instances does not run the static block multiple time. Only the constructor will be executed multiple time.
  • If Class.forName(“class_name“) is called then the static block of the Class will get executed.

Author avatar
JBT
https://javabeginnerstutorial.com/

34 comments

  1. Jayanth

    Nice article it cleared all my doubts

Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.