Learn static keyword in java

Welcome to flower brackets blog. Today we will learn static keyword in java.

In java, keyword “static” belongs to class. When a member variable or method or nested class is declared as static it belongs to class not object.

It can be accessed before creating object. That is, member method can be accessed before any objects of its class are created without any reference to object.

Example:

class Demo
{
   // static method 
   static void display() 
   { 
      System.out.println("in static method"); 
   }
}

public class StaticKeywordDemo 
{
   public static void main(String[] args) 
   {
      Demo.display();
   }
}

Output:

in static method

Meanwhile keyword “static” saves memory by allocating memory only once at the time of class loading.


Static variable in java

We can declare static variable by adding keyword “static” before variable name. Static variables are intrinsically global variables. At the time of class loading static variables are shared at class level only.

public class Employee 
{
   int empID;
   int empSalary;
   // company name is common
   static String company;
   public void print()
   {
      System.out.println(empID + " - " + empSalary + " - " + company);
   }
}

In class “Employee” we have declared static string variable “company” and we access the same in main method like this,

public class StaticVariableDemo
{
   public static void main(String[] args)
   {
      Employee emp1 = new Employee();
      emp1.empID = 8;
      emp1.empSalary = 40000;
      Employee.company = "IBM";

      Employee emp2 = new Employee();
      emp2.empID = 9;
      emp2.empSalary = 45000;
      Employee.company = "IBM";
      emp1.print();
      emp2.print();
   }
}

Output:

8 – 40000 – IBM
9 – 45000 – IBM


Static method in java

We can declare a method as static by adding keyword “static” before method name. Static methods are accessed without creating object.

Static methods can call other static methods and access static data directly.

NOTE

Static method cannot refer super and this.

public class Welcome 
{
   public static String Greetings(String message)
   {
      return message;
   }
 
   public String GreetWithSmile(String message)
   {
      return message;
   }
}

public class StaticMethodDemo 
{
   public static void main(String[] args) 
   {
      Welcome wc = new Welcome();
      Welcome.Greetings("Welcome to Flower Brackets!!");
      wc.GreetWithSmile("Hi!! Flower Brackets.");
   }
}

Here we have class “Welcome” which has two methods static method and other non static method.

Static method means a method declared with “static” keyword. “Greetings” method is taking a string “message” as an argument and it’s returning string “message”.

Also read – switch statement java

And another method which is “GreetWithSmile” and this does not have any static keyword in it.

The static member only belongs to class not to the instance. It will not belong to the instance of class “Welcome”. So if you do something like this,

wc.Greetings("Welcome to Flower Brackets!!");

It will work. But it will give warning like this,

Learn Static Keyword In Java

Warning is “The static method Greetings from the type Welcome should be accessed in a static way”.

We cannot access “Greetings” method which is declared as “static”. You can access “Greetings” method directly like this,

Welcome.Greetings("Welcome to Flower Brackets!!");

But when you try to access non static method ,

Learn Static Keyword In Java

“GreetWithSmile” method is not available in class “Welcome” and cannot be accessed directly with class name.

“Greetings” method is accessed from the class “Welcome” and “GreetWithSmile” method which is not a static method cannot be accessed directly by class “Welcome”.

You can see (in the above image) there is no “GreetWithSmile” method. Non static method is available through instance of class “Welcome”.

Non static methods is accessed using object and static method should be accessed using the class itself not the instance.

Below is the correct way of using non-static method,

wc.GreetWithSmile("Hi!! Flower Brackets.");


When static block is called in java?

Static block is called as soon as class is loaded into JVM. It is also known as Static initialization block. For example,

public class StaticBlockExample
{
   static
   {
      System.out.println("In Static block");
   }
   public static void main(String args[])
   {
      System.out.println("In main method");
   }
}

Output:

In Static block

In main method

As we know the first method which is executed by JVM is “main” method. So it should first print “In main method”. But static block gets called when class is loaded.

That means static block is called before “main” method. As you can see the output “In static method” prints first. So static block always get executed first and then main method.


Why is java main method static?

Because “main” method is static there is no need of object creation. Since main method is “static” JVM calls it without creating instance.


Also read – learn nested classes in java

Related Posts