codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Subjects
🧩

OOP Concepts

23 chapters

1Procedural vs Object-Oriented2Classes, Objects, & Instantiation3Constructors & Destructors4Static Members & Methods5Encapsulation & Access Modifiers6Data Abstraction7Inheritance Types (Single, Multiple)8Compile-Time Polymorphism (Overloading)9Polymorphism & Interfaces10Run-Time Polymorphism (Overriding)11Virtual Functions & V-Tables12Interfaces & Abstract Classes13Generic Programming (Templates & Generics)14Exception Handling in OOP15SOLID Design Principles16Composition over Inheritance17Coupling & Cohesion18UML Diagrams Basics19Creational Patterns (Singleton, Factory)20Structural Patterns (Adapter, Decorator)21Behavioral Patterns (Observer, Strategy)22MVC Architecture Pattern23Object Serialization & Cloning
SubjectsOOP Concepts

Static Members & Methods

Updated 2026-04-23
1 min read

Static Members & Methods

In OOP, most members (fields and methods) belong to a specific object instance. Each object has its own copy of instance variables. Static Members, however, belong to the class itself. There is only one copy, shared across ALL instances.

1. Static Variables (Class Variables)

A static variable is shared by every object of the class. Changing it through one object changes it for all objects.

class Student {
    String name;
    static int totalStudents = 0; // Shared across ALL Student objects

    Student(String name) {
        this.name = name;
        totalStudents++; // Incremented for every new student
    }
}

Student s1 = new Student("Alice"); // totalStudents = 1
Student s2 = new Student("Bob");   // totalStudents = 2
System.out.println(Student.totalStudents); // 2 (accessed via class name)

Use Cases: Counters (tracking how many objects have been created), configuration constants, shared caches.

2. Static Methods

A static method can be called without creating an object. It can only access other static members (not instance variables or instance methods).

class MathUtils {
    static double circleArea(double radius) {
        return Math.PI * radius * radius;
    }
}

double area = MathUtils.circleArea(5.0); // No object needed

Use Cases: Utility functions (Math.sqrt, Collections.sort), factory methods, main() method in Java.

3. Static Blocks

A static initializer block runs once when the class is first loaded into memory, before any object is created.

class Config {
    static Map settings;
    static {
        settings = new HashMap();
        settings.put("timeout", "30");
    }
}

4. Why main() is Static

The main() method in Java is static because the JVM needs to call it before any objects exist. It's the entry point of the program, and at that point, no instances have been created yet.



PreviousConstructors & DestructorsNextEncapsulation & Access Modifiers

Recommended Gear

Constructors & DestructorsEncapsulation & Access Modifiers