Java Tutorial

Java Static

static on a field means one shared copy for the whole class. static on a method means you can call it without creating an object — like Math.max or main itself.

Shared fields

An instance field exists once per object. A static field exists once per class. Every instance sees the same value — useful for counters, constants, and caches.

Example

class Player {
  static int created = 0;
  String name;

  Player(String name) {
    this.name = name;
    created++;
  }
}

public class Main {
  public static void main(String[] args) {
    new Player("Rin");
    new Player("Kai");
    System.out.println(Player.created);   // 2
  }
}

Static methods need no instance

Call a static method on the class: Math.max(3, 7) or Player.describe(). Inside a static method you cannot use instance fields without an object — there is no this.

Example

public class Main {
  static int max(int a, int b) {
    return a > b ? a : b;
  }

  public static void main(String[] args) {
    System.out.println(max(3, 7));
  }
}

Constants with static final

Class-wide constants are usually public static final and named inUPPER_SNAKE_CASE. Everyone reads the same immutable value.

Example

class Config {
  public static final int MAX_USERS = 100;
}

public class Main {
  public static void main(String[] args) {
    System.out.println(Config.MAX_USERS);
  }
}

Overusing static turns objects into bags of global data. Prefer instance fields for per-object state; reach for static when the value or helper truly belongs to the type as a whole.

Try It Yourself

Exercise: Add a static int nextId to a Ticket class. Each constructor assigns id = nextId++. Create two tickets and print both ids and the final nextId.

Show solution
class Ticket {
  static int nextId = 1;
  int id;

  Ticket() {
    id = nextId++;
  }
}

public class Main {
  public static void main(String[] args) {
    Ticket a = new Ticket();
    Ticket b = new Ticket();
    System.out.println(a.id + " " + b.id);   // 1 2
    System.out.println(Ticket.nextId);       // 3
  }
}

The static counter is shared, so each new ticket gets the next id.

Key Takeaways

  • Static fields are shared by every instance of the class.
  • Static methods are called on the class and have no this.
  • static final is the usual pattern for class constants.
  • Use static for type-level data and helpers — not as a substitute for objects.

Worked examples

The short listings above are there so you can see the grammar. The programs here use the same statements on quantities that already have units: a speed, a pH, a count of bases. They are classroom numbers. Air resistance is ignored. g is 9.81 m/s² unless a line says otherwise.

Open them in the Java editor at /java/try. Change one measurement and check whether the result still has the right unit.

Engineering

Shared instance counter

A static field belongs to the class, not one object. Every new Device bumps the same count.

Example

class Device {
  static int count;
  Device() { count++; }
}

public class Main {
  public static void main(String[] args) {
    new Device();
    new Device();
    System.out.println(Device.count);
  }
}

FAQ: Java Static

Common questions about this page.

What is the StudyGrid Java tutorial?

The StudyGrid Java tutorial is a full beginner-to-advanced track: syntax, types, input, loops, methods, classes, collections, generics, maps, and lambdas. Each chapter has copy-and-run examples.

Should I run java static examples locally for better learning?

Yes. Use the browser editor on StudyGrid for a quick check, then Download the example and run it on your computer. Local runs show real errors and the real toolchain, which is one of the fastest ways to learn java static in this Java Java lesson (Java Static).

Is the Java editor the same as Try Python or Try C++?

No. Try Java compiles with javac at /java/try and shows stdout plus compiler messages. Try Python stays at /try. Try C++ stays at /cpp/try. Java lessons never open those editors.

Do I need to install a JDK to learn Java?

No. Open a chapter, click Try it in Java, and compile in the browser. You can also download a .java file and compile locally with javac.

Where should I start the Java tutorial?

Start at Java Intro, then Get Started and Syntax. After the first program, continue to output, variables, and if-else. After classes, open Java Examples, then generics, map, and lambdas. Use Next at the bottom of each chapter.

Is the Java tutorial free?

Yes. The Java workshop on StudyGrid (studygrid.in) is free: dashboard, chapters, and the compile-and-run editor.