Skip to main content

Static Class and Its Usage in Java

2 min read Updated May 29, 2026
Share:
On this page (10sections)

Introduction

Static Class and Its Usage in is a classic Java console program that demonstrates the concept with complete source code and sample output. These programs cover your first Java class, constructors, methods and simple OOP building blocks.

This tutorial walks through the program line by line, explains how the logic works, and highlights best practices you can apply in your own code.

Static Class (Nested) Characteristics In Java

  • A static class can access without creating any of object If it has static data members and methods.
  • A static class cannot access non-static data members and methods.
  • The static class can be accessed by outer class name.
  • Need to create an object for access non-static data members and methods.
  • A static class created inside a class is called nested static class.
  • Only nested classes can be static classes.
  • In Java, we can declare a class within another class. It is called a nested class. That nested class accessed based on Outer class.

Syntax

//Static Class
static class className(){
	//Do something
}

//Static variable
static int variableName = 0;

//Static method inside a static class
static class className(){
	static void staticMethod(){
		//Do something
	}
}

Static Class Example Program

class StaticClass {
    
    public static class StaticClassInner {
    public static int staticNumber = 100;
    
    public static int staticMethod() {
        staticNumber = 200;
        return staticNumber;
    }
}
    
    public static void main(String []args){
        System.out.println("Initial value of Static number : " + StaticClassInner.staticNumber);
        System.out.println("Static number inside static class : " + StaticClassInner.staticMethod());
     }
}

Sample Output

Initial value of Static number : 100
Static number inside static class : 200

When to use

Use this static class and its usage in example when learning or revising core Java syntax.

How it works

  1. Execution begins in the main method — the JVM calls this method when you run the class.

  2. staticNumber = 200; updates a variable used in the calculation or output.

  3. A println / print call writes text to the console — part of the sample output below.

  4. A println / print call writes text to the console — part of the sample output below.

  5. Compare your console output with the sample output for Static Class and Its Usage in to confirm the program behaves correctly.

Best Practices

  • Name classes in PascalCase and follow one public class per file when starting out.
  • Keep main short — delegate work to other methods as programs grow.

Common Mistakes

  • Copying code without understanding each line — practice by changing one statement at a time.
  • Mismatching the public class name and the .java filename.
  • Forgetting semicolons at the end of statements.

Frequently Asked Questions

What does the Static Class and Its Usage in program demonstrate?
It shows how to implement static class and its usage in in Java with a complete runnable example and expected console output.
How do I run this Java program?
Save the code in a `.java` file matching the public class name, compile with `javac`, then run with `java ClassName`.
When would I use this pattern?
Use this pattern whenever you need the same logic in homework, practice or small utility tools.

Related Tutorials

Search tutorials