Java Static

Share

Java Static

In Java, the static keyword is used to define a class member (variable or method) that belongs to the class itself rather than an instance of the class. Here are some key points about using static in Java:

  1. Static Variables: When a variable is declared as static, it is called a static variable (or class variable). These variables are shared by all instances of the class. They are initialized only once, at the start of the program, and any changes made to them are reflected across all instances of the class. Static variables are accessed using the class name, followed by the variable name.

    java
    public class MyClass {
    static int count = 0; // static variable

     

    public MyClass() {
    count++; // accessing static variable
    }
    }

  2. Static Methods: When a method is declared as static, it is called a static method. Static methods belong to the class itself and not to any particular instance of the class. They can be invoked directly using the class name, without creating an instance of the class. Static methods cannot access non-static (instance) variables directly since they are not tied to any particular instance.

    java
    public class MathUtils {
    public static int add(int a, int b) { // static method
    return a + b;
    }
    }

     

    // Invoking the static method without creating an instance
    int result = MathUtils.add(3, 5);

  3. Static Block: In addition to static variables and methods, Java allows the use of a static block, which is a block of code enclosed within curly braces and preceded by the static keyword. The code inside the static block is executed only once when the class is loaded into memory, typically before the main method is called.

    java
    public class MyClass {
    static {
    // Code inside static block
    System.out.println("Static block executed.");
    }

     

    public static void main(String[] args) {
    // Main method
    }
    }

  4. Static Nested Classes: In Java, you can also define a class within another class. If the nested class is declared as static, it becomes a static nested class. Static nested classes are accessed using the outer class name, followed by the nested class name.

    java
    public class OuterClass {
    static class StaticNestedClass {
    // Code for the static nested class
    }
    }

     

    // Accessing the static nested class
    OuterClass.StaticNestedClass nestedObject = new OuterClass.StaticNestedClass();

Remember that static members belong to the class itself and are not associated with any specific instance of the class. They can be accessed using the class name directly, making them useful for creating utility methods, constants, or maintaining shared state across instances of a class.

Demo Day 1 Video:

 
You can find more information about Java in this Java Docs Link

 

Conclusion:

Unogeeks is the No.1 Training Institute for Java Training. Anyone Disagree? Please drop in a comment

You can check out our other latest blogs on Java Training here – Java Blogs

You can check out our Best in Class Java Training details here – Java Training

💬 Follow & Connect with us:

———————————-

For Training inquiries:

Call/Whatsapp: +91 73960 33555

Mail us at: info@unogeeks.com

Our Website ➜ https://unogeeks.com

Follow us:

Instagram: https://www.instagram.com/unogeeks

Facebook: https://www.facebook.com/UnogeeksSoftwareTrainingInstitute

Twitter: https://twitter.com/unogeeks


Share

Leave a Reply

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