Java Class Attributes

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

Java Class Attributes

Describe the state of each object.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

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

Understand the idea

Fields store object or class state. An instance field belongs to each object; a static field is associated with the class. Give fields sensible defaults and avoid exposing every field publicly in real application models.

02 / Step by step

Explore the details

Java Class Attributes

Attributes (also called fields) are variables declared inside a class. Every object created from that class gets its own copy of those attributes.

Declaring and Accessing Attributes

Independent Copies Per Object

Final Fields (Constants)

Use the final keyword to make an attribute a constant — its value is set once and can never be changed after that.

Java reference fragmentJAVA
class Circle {
    final double PI = 3.14159;  // constant — cannot be changed
    double radius;

    double area() {
        return PI * radius * radius;
    }
}

Circle c = new Circle();
c.radius = 5;
System.out.println(c.area());  // 78.53975

// c.PI = 3.0;  // ERROR: cannot assign a value to final variable PI

Use final to make an attribute constant — its value can never be changed after it is set: final double PI = 3.14159;

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 {
        Lamp desk = new Lamp();
        Lamp floor = new Lamp();
        desk.on = true;
        System.out.println(desk.on);
        System.out.println(floor.on);
    }
}
class Lamp { boolean on; }
Predict first. Then reveal the expected output / வெளியீடு
true
false

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

Set floor.on to true and print both values again.

Reveal the answer and reasoning / விடை

Both print true because you explicitly changed the second object as well.

One mistake to avoid: An uninitialized boolean instance field defaults to false, but an uninitialized local variable cannot simply be read.

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.