Static Method in Java With Examples

16 Nov, 2021
The static keyword is used to construct methods that will exist regardless of whether or not any instances of the class are generated. Any method that uses the static keyword is referred to as a static method.
Features of static method:
A static method in Java is a method that is part of a class rather than an instance of that class.
Every instance of a class has access to the method.
Static methods have access to class variables (static variables) without using the class’s object (instance).
Only static data may be accessed by a static method. It is unable to access data that is not static (instance variables).
In both static and non-static methods, static methods can be accessed directly.
Syntax to declare the static method:
Access_modifier static void methodName()
{ 
     // Method body.
} 
The name of the class can be used to invoke or access static methods.
Syntax to call a static method:
className.methodName(); 
The JVM runs the static method first, followed by the creation of class instances. Because no objects are accessible when the static method is used. A static method does not have access to instance variables. As a result, a static method can’t access a class’s instance variable.
Java
// Java program to demonstrate that// The static method does not have// access to the instance variable  import java.io.*;  public class GFG {    // static variable    static int a = 40;      // instance variable    int b = 50;      void simpleDisplay()    {        System.out.println(a);        System.out.println(b);    }      // Declaration of a static method.    static void staticDisplay()    {       System.out.println(a);     }      // main method    public static void main(String[] args)    {        GFG obj = new GFG();        obj.simpleDisplay();          // Calling static method.        staticDisplay();    }}
40
50
40
Java
// Java program to demonstrate that// In both static and non-static methods,// static methods are directly accessed.  import java.io.*;  public class StaticExample {        static int num = 100;    static String str = "GeeksForGeeks";      // This is Static method    static void display()    {        System.out.println("static number is " + num);        System.out.println("static string is " + str);    }      // non-static method    void nonstatic()    {        // our static method can accessed         // in non static method        display();    }      // main method    public static void main(String args[])    {        StaticExample obj = new StaticExample();                // This is object to call non static function        obj.nonstatic();                // static method can called         // directly without an object        display();    }}
static number is 100
static string is GeeksForGeeks
static number is 100
static string is GeeksForGeeks
To access and change static variables and other non-object-based static methods.Utility and assist classes frequently employ static methods.
To access and change static variables and other non-object-based static methods.
Utility and assist classes frequently employ static methods.
Non-static data members or non-static methods cannot be used by static methods, and static methods cannot call non-static methods directly.In a static environment, this and super aren’t allowed to be used.
Non-static data members or non-static methods cannot be used by static methods, and static methods cannot call non-static methods directly.
In a static environment, this and super aren’t allowed to be used.
It’s because calling a static method isn’t needed of the object. If it were a non-static function, JVM would first build an object before calling the main() method, resulting in an extra memory allocation difficulty.
Instance Methods
Static Methods
Java-Object Oriented
Picked
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Stream In Java
Introduction to Java
Constructors in Java
Exceptions in Java
Generics in Java
Functional Interfaces in Java
Java Programming Examples
Strings in Java
Differences between JDK, JRE and JVM
Abstraction in Java