Java Tutorial

Java final

final locks a variable to one assignment, a method against overrides, or a class against subclasses — three related ideas, one keyword.

final variables

Mark a variable final when it must never be reassigned after its first assignment. The compiler enforces it — ideal for constants, fixed configuration, and values you want to treat as read-only.

Example

public class Main {
  public static void main(String[] args) {
    final double TAX = 0.2;
    double price = 50.0;
    System.out.println(price * (1 + TAX));
    // TAX = 0.25;   // error: cannot assign a value to final variable
  }
}

final references vs mutable objects

A final reference cannot point somewhere else, but the object it points to may still change — unless that object is itself immutable.

Example

import java.util.ArrayList;
import java.util.List;

public class Main {
  public static void main(String[] args) {
    final List names = new ArrayList<>();
    names.add("Ada");          // OK — list contents change
    // names = new ArrayList<>();  // error — cannot reassign final
    System.out.println(names);
  }
}

Class constants are usually public static final and named inUPPER_SNAKE_CASE: public static final int MAX = 100;.

final methods

A final method cannot be overridden in a subclass. Use it when a piece of behavior must stay exactly as written in the parent.

Example

class Account {
  final String id() {
    return "ACC";
  }
}
class Savings extends Account {
  // String id() { return "SAV"; }   // error: cannot override final method
}
public class Main {
  public static void main(String[] args) {
    System.out.println(new Savings().id());
  }
}

final classes

A final class cannot be extended. String is final for that reason — the platform guarantees its behavior. Your own domain types can be final when inheritance would only invite misuse.

Example

final class Token {
  final String value;
  Token(String value) { this.value = value; }
}
// class FakeToken extends Token {}   // error: cannot inherit from final class
public class Main {
  public static void main(String[] args) {
    System.out.println(new Token("abc").value);
  }
}

final on a field of a reference type does not freeze the object’s interior — only the reference. For true immutability, use unmodifiable collections or records with immutable components.

Try It Yourself

Exercise: Declare a final int MAX_SCORE = 100 and print whether a variable score is within range (0 to MAX_SCORE inclusive).

Show solution
final int MAX_SCORE = 100;
int score = 87;
boolean ok = score >= 0 && score <= MAX_SCORE;
System.out.println(ok);

MAX_SCORE cannot be reassigned, so the upper bound stays fixed for the rest of the method.

Key Takeaways

  • final variables get one assignment; reassignment is a compile error.
  • A final reference can still point at a mutable object.
  • final methods cannot be overridden; final classes cannot be subclassed.
  • Use static final UPPER_SNAKE constants for shared fixed values.

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.

Physics

Planck constant once

final marks a value you must not reassign. Physical constants belong here so a later line cannot overwrite h.

E = hf

Example

public class Main {
  public static void main(String[] args) {
    final double h = 6.626e-34;
    double f = 5.00e14;
    System.out.println(h * f);
  }
}

FAQ: Java final

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 final 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 final in this Java Java lesson (Java final).

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.