Static Keyword in Java

- Mainly used for Memory Management
- Static variables are stored in non-heap memory, Non-static variables are stored in heap memory



- Usages
        1. Block
        2. Variable
        3. Method
        4. Nested class

Static Block

- It's executed exactly once when the class is first loaded
- It's executed before the main method at the time of classloading.

Example:

public class StaticKeyword {

static
{
         System.out.println("Static block initialized.");
}

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


Response:

Static block initialized.
Main Method


Static Variable

- It's used for a constant variable or a method that is same for every instance of a class
- It can be used to refer to the common property of all objects
- It can be created at class-level only. (Not allowed to create a static local variable in member function in java)

Example:

public class StaticKeyword {

static
{
System.out.println("Static block initialized.");
}

void show() //Local variable cannot be a static variable
{
static int a = 2; //Compile issue
}

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

}

Example: It's used for a constant variable or a method that is same for every instance of a class

public class StaticKeyword {

public static void main(String[] args) {
Employee e1 = new Employee("Mani", "Raj");
Employee e2 = new Employee("Vino", "Kumar");

Employee.manager = "Manik";

e1.show();
e2.show();
}
}

class Employee {
String name;
static String manager;

Employee(String name, String manager)
{
this.name = name;
this.manager = manager;
}

void show()
{
System.out.println("name - "+name);
System.out.println("manager - "+manager);
}

}

Response:
name - Mani
manager - Manik
name - Vino
manager - Manik


(The counter program for best example for static variable)

Static Method:

- They can access static data directly. No need to create an object for the class
    Ex: StaticKeyword.show();
- A static method can access only static variables

Example:

public static void main()

Ex:

public class StaticKeyword {

static int a = 10;

public static void main(String[] args) {
System.out.print(a); //Not static variable not allowed in static method
}

}



Static Nested class:

- A class can be made static only if it is a nested class.

Difference between nested static classes and non-static (inner) classes


public class StaticTutorial {

 class NestedClass
 {
  int a = 10;

  void show()
  {
   System.out.println("Var a "+a);
  }

 }


 static class StaticNestedClass
 {

  int b = 20;

  void display(){
   System.out.println("Var b "+b);
  }

 }

 public static void main(String[] args) {
  System.out.println("Main Method");

//Nested non-static class
//  StaticTutorial st = new StaticTutorial();
//  NestedClass nc = st.new NestedClass();
//  nc.show();

  StaticTutorial.NestedClass st = new StaticTutorial(). new NestedClass();
  st.show();


//Nested static class
//  StaticTutorial.StaticNestedClass st = new StaticTutorial.StaticNestedClass();
//  st.display();
 }
}



Enjoy Coding... 😉






Comments

Popular posts from this blog

Google Assistant Implementation in Android application with app actions