Java Modifiers

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 29 / 50 · English

Java Modifiers

Make ownership and access intentional.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Modifiers.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

Modifiers answer different questions. public/private affect access; static affects class versus instance membership; final prevents reassignment of a variable. A final object reference does not make the referenced object deeply immutable.

02 / Step by step

Explore the details

Modifiers

By now, you are quite familiar with the public keyword that appears in almost all of our examples:

The public keyword is an access modifier, meaning that it is used to set the access level for classes, attributes, methods and constructors.

We divide modifiers into two groups:

  • Access Modifiers – controls the access level
  • Non-Access Modifiers – do not control access level, but provides other functionality

Access Modifiers

For classes, you can use either public or default:

For attributes, methods and constructors, you can use the one of the following:

Public vs. Private Example

In the example below, the class has one public attribute and one private attribute.

Think of it like real life:

  • public – a public park, everyone can enter
  • private – your house key, only you can use it

Example explained

Here, name is declared as public, so it can be accessed from outside the Person class. But age is declared as private, so it can only be used inside the Person class.

Non-Access Modifiers

Non-access modifiers don't control visibility — they change how a member behaves.

ModifierApplies toEffect
staticFields & methodsBelongs to the class, not to any object. Call it without creating an instance.
finalFields, methods, classesField: value can't change. Method: can't be overridden. Class: can't be extended.
abstractMethods & classesMethod has no body — subclass must implement it. Class can't be instantiated.
synchronizedMethodsOnly one thread can execute this method at a time (thread safety).
transientFieldsField is skipped when serializing the object to a file/stream.
volatileFieldsValue is always read from main memory, not from a thread's local cache.
Java reference fragmentJAVA
class MathConst {
    // static: shared by all instances, accessed via class name
    static final double PI = 3.14159;

    // static method: no object needed
    static double circleArea(double r) {
        return PI * r * r;
    }
}

// abstract: class cannot be instantiated — must be subclassed
abstract class Shape {
    abstract double area();   // no body — subclass provides it
}

class Square extends Shape {
    double side;
    Square(double s) { this.side = s; }

    @Override
    double area() { return side * side; }  // concrete implementation
}

System.out.println(MathConst.PI);               // 3.14159
System.out.println(MathConst.circleArea(5));    // 78.53975
System.out.println(new Square(4).area());       // 16.0
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        System.out.println(Settings.LANGUAGE);
        System.out.println(Settings.doubleValue(4));
    }
}
class Settings {
    static final String LANGUAGE = "Java";
    static int doubleValue(int n) { return n * 2; }
}
Predict first. Then reveal the expected output / வெளியீடு
Java
8

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Try reassigning Settings.LANGUAGE, then undo that change.

Reveal the answer and reasoning / விடை

The assignment fails to compile because the field is final.

One mistake to avoid: static and public are different concepts: being static does not make a member accessible everywhere.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.