Java Static Keyword – Master the Concept of Static Methods in Java

In Java using a static keyword, we can have a static method, static class, static block, static variable. This keyword is mainly used for the management of memory. We can use static keyword with methods, variables, class, and blocks. It belongs to a class rather than the instance of the class, It simply means that if you make any variable static to can access it without its object.

Static can be:

  1. Block
  2. Variable (also known as a class variable)
  3. Method (also known as a class method)
  4. Class

Java-Static-Method-Class-Variable-Block

Don’t forget to check important Keywords in Java

1. Static Block in Java

A static block initializes the static variables. It executes whenever the class is loaded in memory. One class can have numerous static blocks, which will be executed in the same sequence in which they are written.

Example-

package com.dataflair.statickeyword;
public class StaticBlock 
{
   static String sentence;
   static int number;
   static
    {
         sentence = "Welcome to DataFlair";
         number = 69;
    }
    public static void main(String args[])
    {
         System.out.println(sentence);
         System.out.println("Value of Integer: "+number);
    }
}

Output-

Java Static Block with Example

Let’s revise the concept of Variables in Java

2. Static Variable in Java

Any variable which is declared using ‘static’ keyword is a static variable. Memory allocation for a static variable happens only once in the class area when the class is loaded in the memory. It is also known as a class variable. It is common to all the objects of the class.

In this, a single copy of a static variable is created and shared among all the objects of the class.

Example-

package com.dataflair.statickeyword;
class Employee
 { 
   int empId; 
   String empName; 
   static String companyName = "TCS"; 
   //constructor to initialize the variable 
   Employee(int id, String name){ 
      empId = id; 
      empName = name; 
   } 
   //method to display values 
   void display()
   {
      System.out.println(empId+" "+empName+" "+companyName);
   } 
} 
//class to create and display the values of object 
public class StaticVariable 
{ 
      public static void main(String args[])
      { 
         Employee EmployeeObj = new Employee(218,"Kushal"); 
         Employee EmployeeObj1 = new Employee(635,"Bhumika"); 
         Employee EmployeeObj2 = new Employee(147,"Renuka"); 
         //calling display method 
         EmployeeObj.display(); 
         EmployeeObj1.display(); 
         EmployeeObj2.display(); 
      } 
}

Output-

Java Static Variable with Example

3. Static Methods in Java

Static methods in Java belong to classes, unlike other classes it doesn’t belong to the instance of the class. This method can be accessible to every instance but the methods defined in the instance are only accessed by that member of the class. It can access only static data.

Join DataFlair on Telegram
  1. These are declared with the keyword “static” when defining a method
  2. This method belongs to the class and not to the object.
  3. It can access only static data. It can not access instance variables.
  4. A static method can call only static methods, non-static methods are not called by a static method.
  5. This method is can be accessed by the class name it doesn’t need any object.

Example of Java Static Methods-

//Java Program to demonstrate the use of a static method.
package com.dataflair.staticmethod;
class Employee
{ 
   int empId; 
   String empName; 
   static String companyName = "TCS"; 
   //static method to valueChange the value of static variable 
   static void valueChange()
   { 
      companyName = "DataFlair"; 
   } 
   //constructor to initialize the variable 
   Employee(int id, String name){ 
      empId = id; 
      empName = name; 
   } 
//method to display values 
void display()
   {
      System.out.println(empId+" "+empName+" "+companyName);
   } 
} 
//class to create and display the values of object 
public class Demo 
{ 
   public static void main(String args[])
      { 
         Employee.valueChange();//calling valueChange method 
         //creating objects 
         Employee EmployeeObj = new Employee(218,"Kushal"); 
         Employee EmployeeObj1 = new Employee(635,"Bhumika"); 
         Employee EmployeeObj2 = new Employee(147,"Renuka"); 
         //calling display method 
         EmployeeObj.display(); 
         EmployeeObj1.display(); 
         EmployeeObj2.display(); 
      } 
}

Output-

Java Static Method with Example

Before we start the Static Class, you should know Inner Classes in Java

4. Java Static Class

Java allows us to define a class but not a static class which simply means that we can define a class static only if it is a nested class. In Java, a class can be static when it is a nested class. Nested static class doesn’t need any reference of the Outer class. Non-static members are not accessed by a static class.

Example-

// Java program to demonstrate how to implement static class
package com.dataflair.statickeyword;
class OuterClass{ 
      private static String messageToReaders = "Hello Readers! Welcome to DataFlair"; 
      // Static nested class 
      public static class NestedStaticClass{ 
            // static class 
            public void myMessage() 
            { 
                  System.out.println("Message from nested static class: " + messageToReaders); 
            } 
      } 
      // non-static nested class 
      public class InnerClass{ 
            public void display(){ 
            System.out.println("Message from non-static nested class: "+ messageToReaders); 
            } 
      } 
} 
public class StaticClass {
      public static void main(String args[]){ 
      // instance of nested Static class 
      OuterClass.NestedStaticClass printer = new OuterClass.NestedStaticClass(); 
      // call non static method of nested static class 
      printer.myMessage(); 
      OuterClass outer = new OuterClass(); 
      OuterClass.InnerClass inner = outer.new InnerClass(); 
      // calling non-static method of Inner class 
      inner.display(); 
      OuterClass.InnerClass innerObject = new OuterClass().new       InnerClass(); 
      innerObject.display(); 
      } 
}

Output-

Java Static Class with Example

What is Java Instance Method?

An instance method in Java is basically a method of the class. In other words, a non-static method which is declared inside a class is an instance method. This kind of method requires an object of its class to created before it can be called. To invoke an instance method, we have to create the object of the class.

public void Dataflair(String name)
{
// code to be executed....
}

Instance Methods vs Static Methods in Java

  1. The instance method requires the object of its class to be created before it can be called. Static methods in Java can be called without creating the object of the class.
  2. Java static method is declared using static keyword. An instance method does not need any keyword.
  3. The static method has a single copy for a class. But instance methods have multiple copies depending on the number of instances created for that particular class.
  4. We can invoke a static method by using its class reference. An instance method is invoked by using the object reference.
  5. We can’t access instance methods and instance variables with the help of Static methods in Java. We can access static variables and static methods with the help of the Instance method.

Learn the most effective ways to implement Singleton Class in Java

Summary

It’s time to summarize the Java static keyword. We can use this feature in static block, static variable, static class and static methods in Java. In addition, you got to know the main difference between Instance methods and static methods in Java.

We hope you like the explanation.

Please drop all the queries and suggestions in the comment section.

Leave a Reply

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

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.