[java] What does the 'static' keyword do in a class?

Static in Java:

Static is a Non Access Modifier. The static keyword belongs to the class than instance of the class. can be used to attach a Variable or Method to a Class.

Static keyword CAN be used with:

Method

Variable

Class nested within another Class

Initialization Block

CAN'T be used with:

Class (Not Nested)

Constructor

Interfaces

Method Local Inner Class(Difference then nested class)

Inner Class methods

Instance Variables

Local Variables

Example:

Imagine the following example which has an instance variable named count which in incremented in the constructor:

package pkg;

class StaticExample {
    int count = 0;// will get memory when instance is created

    StaticExample() {
        count++;
        System.out.println(count);
    }

    public static void main(String args[]) {

        StaticExample c1 = new StaticExample();
        StaticExample c2 = new StaticExample();
        StaticExample c3 = new StaticExample();

    }
}

Output:

1 1 1

Since instance variable gets the memory at the time of object creation, each object will have the copy of the instance variable, if it is incremented, it won't reflect to other objects.

Now if we change the instance variable count to a static one then the program will produce different output:

package pkg;

class StaticExample {
    static int count = 0;// will get memory when instance is created

    StaticExample() {
        count++;
        System.out.println(count);
    }

    public static void main(String args[]) {

        StaticExample c1 = new StaticExample();
        StaticExample c2 = new StaticExample();
        StaticExample c3 = new StaticExample();

    }
}

Output:

1 2 3

In this case static variable will get the memory only once, if any object changes the value of the static variable, it will retain its value.

Static with Final:

The global variable which is declared as final and static remains unchanged for the whole execution. Because, Static members are stored in the class memory and they are loaded only once in the whole execution. They are common to all objects of the class. If you declare static variables as final, any of the objects can’t change their value as it is final. Therefore, variables declared as final and static are sometimes referred to as Constants. All fields of interfaces are referred as constants, because they are final and static by default.

enter image description here

Picture Resource : Final Static

Examples related to java

Under what circumstances can I call findViewById with an Options Menu / Action Bar item? How much should a function trust another function How to implement a simple scenario the OO way Two constructors How do I get some variable from another class in Java? this in equals method How to split a string in two and store it in a field How to do perspective fixing? String index out of range: 4 My eclipse won't open, i download the bundle pack it keeps saying error log

Examples related to static

What is the equivalent of Java static methods in Kotlin? Creating a static class with no instances Static vs class functions/variables in Swift classes? Call static methods from regular ES6 class methods What is the difference between static func and class func in Swift? An object reference is required to access a non-static member Mocking static methods with Mockito @Autowired and static method The static keyword and its various uses in C++ Non-Static method cannot be referenced from a static context with methods and variables

Examples related to oop

How to implement a simple scenario the OO way When to use 'raise NotImplementedError'? PHP: cannot declare class because the name is already in use Python class input argument Call an overridden method from super class in typescript Typescript: How to extend two classes? What's the difference between abstraction and encapsulation? An object reference is required to access a non-static member Java Multiple Inheritance Why not inherit from List<T>?

Examples related to language-features

What is the python "with" statement designed for? How to Correctly Use Lists in R? When do I need to use Begin / End Blocks and the Go keyword in SQL Server? How to loop through all enum values in C#? What's the difference between interface and @interface in java? Is there more to an interface than having the correct methods What does the 'static' keyword do in a class? JavaScript hashmap equivalent Why Doesn't C# Allow Static Methods to Implement an Interface?

Examples related to restriction

What does the 'static' keyword do in a class?