Java Class Attributes
Describe the state of each object.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Class Attributes.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
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.
விரிவான விளக்கங்கள்
Java Class Attributes
Class attributes என்பது class-க்கு உள்ளே இருக்குற variables — fields என்றும் சொல்வோம்.
Attribute values-ஐ access பண்ண dot (.) syntax use பண்றோம்:
Attributes Modify பண்றது
Attribute value-ஐ மாத்துவது:
final Attributes
Attribute மாறவே கூடாதுன்னா final பயன்படுத்தலாம்:
Final Fields (Constants)
class Circle {
final double PI = 3.14159; // constant
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 to final variableJava-வில் `static` variable என்றால் என்ன?
- ஒரு object-க்கு மட்டும் சொந்தமானது
- Class-க்கே சொந்தமானது — எல்லா objects-உம் share செய்யும்
- Constant variable
- Private variable
Reveal answer / விடை
Class-க்கே சொந்தமானது — எல்லா objects-உம் share செய்யும்
`static` variables class-level-ல் இருக்கும் — எல்லா instances-உம் ஒரே value share செய்யும். Example: `Student.count` — எத்தனை students create ஆனார்கள் என்று track செய்ய.
முழுமையான உதாரணம்
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.
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.
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.
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.
நீங்களே முயற்சி செய்யுங்கள்
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.
தவிர்க்க வேண்டிய தவறு: An uninitialized boolean instance field defaults to false, but an uninitialized local variable cannot simply be read.
பாடத்தின் சுருக்கம்
- 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.