Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 5 of 10

Static Members - Class-Level Data & Methods

Static fields and methods belong to the class, not to any instance. They are shared across all objects. Knowing when to use static (and when NOT to) is one of the most important Java design decisions.

12 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Static Fields & Methods

Static Fields & Methods
public class Book {
    // Static field - shared across ALL Book instances
    private static int totalBooksCreated = 0;
    private static final String LIBRARY_NAME = "City Library"; // constant: final static

    private final String isbn;
    private String title;
    private double price;

    public Book(String isbn, String title, double price) {
        this.isbn  = isbn;
        this.title = title;
        this.price = price;
        totalBooksCreated++;  // incremented for every new Book
    }

    // Static method - belongs to class, can't access 'this' or instance fields
    public static int getTotalBooksCreated() {
        return totalBooksCreated;
    }

    public static String getLibraryName() {
        return LIBRARY_NAME;
    }

    // Static factory method - named alternative to constructor (pattern)
    public static Book freeBook(String isbn, String title) {
        return new Book(isbn, title, 0.0);
    }

    // Instance method - can access static AND instance members
    public String getSummary() {
        return String.format("[%s] %s @ $%.2f", LIBRARY_NAME, title, price);
    }

    public static void main(String[] args) {
        System.out.println(Book.getLibraryName());    // City Library  - called on Class
        System.out.println(Book.getTotalBooksCreated()); // 0

        Book b1 = new Book("001", "Clean Code", 39.99);
        Book b2 = new Book("002", "Effective Java", 44.99);
        Book free = Book.freeBook("000", "Student Guide");

        System.out.println(Book.getTotalBooksCreated()); // 3
        System.out.println(b1.getSummary());  // [City Library] Clean Code @ $39.99
    }
}

When NOT to use static

  • Static fields are shared state - a bug that changes a static field affects all instances across the app. Avoid mutable static fields in multi-threaded code.
  • Overusing static methods creates procedural, not object-oriented code. Don't make every helper method static.
  • Static fields make unit testing harder - they persist between tests and can cause order-dependent test failures.
  • Good uses of static: constants (static final), factory methods, utility/helper methods (like Math.abs)

Quick Quiz

Tip

Tip

Practice Static Members ClassLevel Data Methods in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.

Diagram
Loading diagram…

Visual architecture and concepts for Static Members - Class-Level Data & Methods

Practice Task

Note

Practice Task - (1) Write a working example of Static Members ClassLevel Data Methods from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.

Common Mistake

Warning

A common mistake with Static Members ClassLevel Data Methods is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready java code.

Key Takeaways

  • Static fields and methods belong to the class, not to any instance.
  • Static fields are shared state - a bug that changes a static field affects all instances across the app. Avoid mutable static fields in multi-threaded code.
  • Overusing static methods creates procedural, not object-oriented code. Don't make every helper method static.
  • Static fields make unit testing harder - they persist between tests and can cause order-dependent test failures.

Topics in This Module

0/10 done