Static Variables

 
/**
 * A static variables is initialized only once.
 * It is common to all objects created with that class
 * It can be called without object instantiation
 * 
 * Static variable usage: Students entering a classroom.
 * Each time a new student is created, we increase a shared counter.
 */

package com.minte9.oop.static_keyword;

public class StaticVariables {
    public static void main(String[] args) {
        ClassRoom cs = new ClassRoom();

        cs.newEntry(new Student("Mary"));
        System.out.println(ClassRoom.totalStudents);  // 1 (accessed in static way)

        cs.newEntry(new Student("John"));
        System.out.println(ClassRoom.totalStudents);  // 2
    }
}

class ClassRoom {
    public static int totalStudents = 0;  // Look Here

    public void newEntry(Student student) {
        totalStudents++;
    }
}

class Student {
    public Student(String name) {}
}

Static Methods

 
/**
 * Static methods belong to the class and not every instance.
 * You do not need a specific object in order to use static methods.
 * Omitting public keyword makes the class, method or property package-private.
 */

package com.minte9.oop.static_keyword;

public class StaticMethods {
    public static void main(String[] args) {

        System.out.println(CurrencyConverter.dollarsToEuro(10));  // 9.0
        System.out.println(CurrencyConverter.dollarsToEuro(50));  // 45.0
    }
}

class CurrencyConverter {
    static double dollarsToEuro(double amount) {
        return amount * 0.9;  // simplified exchange rate
    }
}

Static Classes

 
/**
 * Only nested classes can be static.
 * We can use the nested class without an instance for outer class.
 */

package com.minte9.oop.static_keyword;

public class StaticClasses {
    public static void main(String[] args) {
        Car.Manual.showInfo();  // 220
    }
}

class Car {
    static int maxSpeed = 220;  // general for this car model

    // static inner class
    static class Manual {  // Look Here
        public static void showInfo() {
            System.out.println(maxSpeed);
        }
    }
}

Final Keyword

 
/**
 * Final classes cannot be extended into different kind.
 * Final methods cannot be changed by subclasses.
 * Final static fields are constants (it can never change).
 */

package com.minte9.oop.static_keyword;

public class FinalKeyword {
    public static void main(String[] args) {
        
        IDCard id = new IDCard();
        id.showInfo();  // Official ID Card

        // ❌ Not allowed
        // IDCard.LABEL = "New ID Card";
    }
}

final class IDCard {  // Look Here
    final static String LABEL = "Official ID Card"; // Look Here
    
    public void showInfo() {
        System.out.println(LABEL);
    }
}

// ❌ Not allowed
// class StudentCard extends IDCard() {}






Questions and answers:
Clink on Option to Answer




1. What does a static variable in Java represent?

  • a) A separate copy for each object
  • b) One shared variable for the entire class

2. How can a static method be called?

  • a) Directly using the class name
  • b) Only through an object

3. What is true about static methods?

  • a) They belong to the class, not individual objects
  • b) They can access non-static variables directly

4. Which statement about static nested classes is TRUE?

  • a) They require an instance of the outer class
  • b) They can be used without an instance of the outer class


References: