Java Tutorial

Operators

Flow Control

String

Number and Date

Built-in Classes

Array

Class and Object

Inheritance and Polymorphism

Exception Handling

Collections, Generics and Enumerations

Reflection

Input/Output Stream

Annotation

Java static Keyword (static Variables And static Methods)

In Java, the static keyword is used to indicate that a member (variable, method, or inner class) belongs to the class itself, rather than to an instance of the class. Static members are shared among all instances of the class, meaning that if one instance modifies the value of a static variable, it will be reflected in all other instances as well. Static methods can be called without creating an instance of the class.

In this tutorial, we will cover the use of the static keyword with variables, methods, and inner classes.

1. Static Variables

Static variables are class-level variables that have only one copy shared among all instances of the class. They can be accessed directly through the class name without creating an instance.

Example of a static variable:

public class Counter {
    private static int count = 0;

    public Counter() {
        count++;
    }

    public static int getCount() {
        return count;
    }
}

public class Main {
    public static void main(String[] args) {
        Counter c1 = new Counter();
        Counter c2 = new Counter();
        Counter c3 = new Counter();

        System.out.println("Count: " + Counter.getCount());
    }
}

In this example, we have a Counter class with a static variable count. Each time a new instance of Counter is created, the constructor increments the static count variable. When we print the count, it shows the total number of instances created.

2. Static Methods

Static methods are class-level methods that can be called without creating an instance of the class. They can only access static variables and invoke other static methods.

Example of a static method:

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

    public static int multiply(int a, int b) {
        return a * b;
    }
}

public class Main {
    public static void main(String[] args) {
        int sum = MathUtils.add(3, 5);
        int product = MathUtils.multiply(3, 5);

        System.out.println("Sum: " + sum);
        System.out.println("Product: " + product);
    }
}

In this example, we have a MathUtils class with two static methods: add() and multiply(). We can call these methods directly using the class name without creating an instance of MathUtils.

3. Static Inner Classes

Static inner classes are nested classes declared with the static keyword. They don't have access to the instance variables and methods of the outer class, but they can access the static members of the outer class.

Example of a static inner class:

public class OuterClass {
    private static String staticMessage = "Hello from the outer class";

    public static class StaticNestedClass {
        public void printMessage() {
            System.out.println(staticMessage);
        }
    }
}

public class Main {
    public static void main(String[] args) {
        OuterClass.StaticNestedClass nestedInstance = new OuterClass.StaticNestedClass();
        nestedInstance.printMessage();
    }
}

In this example, we have an OuterClass with a static inner class StaticNestedClass. The StaticNestedClass has a method printMessage() that can access the static variable staticMessage of the OuterClass. We can create an instance of the static inner class without creating an instance of the outer class.

In this tutorial, we covered the basics of the static keyword in Java and how to use it with variables, methods, and inner classes.

  1. Java Static Methods and Their Use Cases:

    • Definition and use cases of static methods.
    public class MyClass {
        public static void staticMethod() {
            // Static method implementation
        }
    }
    
  2. Benefits of Using Static Variables in Java:

    • Advantages of using static variables for shared data.
    public class SharedData {
        public static int count = 0;
    }
    
  3. Static vs Instance Variables in Java:

    • Understanding the difference between static and instance variables.
    public class MyClass {
        static int staticVar;
        int instanceVar;
    }
    
  4. Java Static Methods vs Instance Methods:

    • Comparing static and instance methods.
    public class MyClass {
        static void staticMethod() { /* ... */ }
        void instanceMethod() { /* ... */ }
    }
    
  5. Static Initializer Block in Java:

    • Using a static initializer block for static variable initialization.
    public class MyClass {
        static {
            // Static initializer block
        }
    }
    
  6. Java Static Block vs Constructor:

    • Differences between a static block and a constructor.
    public class MyClass {
        static {
            // Static block
        }
        MyClass() {
            // Constructor
        }
    }
    
  7. Java Static Variables and Initialization:

    • Understanding the initialization of static variables.
    public class Configuration {
        public static final String SERVER_URL;
        static {
            SERVER_URL = "http://example.com";
        }
    }
    
  8. Java Static Import for Methods:

    • Using static import for cleaner code.
    import static java.lang.Math.*;
    public class MathOperations {
        double result = sqrt(25);
    }
    
  9. Static Variables in Inheritance in Java:

    • Dealing with static variables in an inheritance hierarchy.
    public class Parent {
        static int sharedValue = 10;
    }
    
    public class Child extends Parent {
        // Accessing sharedValue
        int childValue = sharedValue;
    }