Static Keyword

In general, Static means staying in one place without moving, or not changing for a long time. For example, your home will be static as it can not be moved. Or In computing it can be understand as a process or variable not able to be changed during a set period, for example while a program is running.

Let’s take an example of an Induction with single cook top. At any given time, it can be utilized for one preparation at a time and another task can not be done until the previous one completes. Let’s explore this more with a recipe!- Imagine preparing a ramen noodle bowl, You’d boil the ramen in a boiling pan and then use another utensil to initiate it’s side preparation. At one time, the cook top will either be used to boil the noodles or to initiate side preparation. You can’t do both at the same time, creating a mechanism where another task gets executed only when the first one is completed/replaced and the space (the cook top!) is free.

In computing, Thumb rule to use static keyword, when you don’t want multiple copies in memory. The static modifier can be applied to fields and methods. If the static keyword is used then fields or method will exist independently of any instances of a classAll static member gets loaded in the memory before the instantiate of the object.

There will be only one copy of a static member in the stack regardless of the number of instances of that class. For example, there is a Test class with “i” and “x” variable static and non static respectively. Test t1, Test t2 and Test t3Static keyword in JavaStatic Keyword Rules

  • Variable or Methods:  marked static belong to the Class rather than to any particular Instance.In fact,you can use a static method or variable without having any instance of that class at all. You need only have the class available to be able to invoke a static method or access a static variable.
  • Static Method or variable: can be used without creating or referencing any 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 nonstatic variable nor can directly invoke non static Method (It can invoke or access Method or variable via instances).
  • The way we access a static method (or static variable) is to use the dot operator on the class name,as opposed to using it on a reference to an instance.
  • 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 can not access an instance variable.
  • Static Methods can not be overridden 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 execute 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.

 

Example:

class Animal {

static void doStuff(){

System.out.println(“Hello”);

}

}

Class Dog extends Animal {

Static void doStuff(){

System.out.println(“world”);

}

Public static void main(String args[]) {

Animal []a = { new Animal () , new Dog () , new Animal () };

for( int x=0 ; x<a.length ; x++){

a[x].doStuff(); //invoke the static method

}

Dog.doStuff();                               //invoke using the class name

}

}

 

Output is :

Hello Hello Hello world

 

Class Variables – Static Fields

Class variables also known as static fields which  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 working example check the Git repo of P3lang.

%d bloggers like this: