Java Modifiers
Make ownership and access intentional.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Modifiers.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
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.
விரிவான விளக்கங்கள்
Java Modifiers என்னன்னு தெரியுமா?
Modifiers என்பது classes, methods, attributes-ஓட access-ஐ control பண்றதுக்கு use ஆகுது.
Access Modifiers
| Modifier | விளக்கம் |
|---|---|
| public | எல்லாரும் access பண்ணலாம் |
| private | அந்த class-ல மட்டுமே access பண்ணலாம் |
| protected | அதே package அல்லது subclass-ல access பண்ணலாம் |
| (default) | அதே package-ல மட்டுமே access பண்ணலாம் |
Non-Access Modifiers
final— class inherit ஆகாது, method override ஆகாது, variable மாறாதுstatic— class-க்கு சொந்தமானது, object-க்கு இல்லabstract— abstract class அல்லது method-க்கு
Non-Access Modifiers
| Modifier | Applies to | Effect |
|---|---|---|
static | Fields, methods | Belongs to the class, not an instance |
final | Classes, methods, variables | Cannot be extended, overridden, or changed |
abstract | Classes, methods | No body — must be implemented by a subclass |
synchronized | Methods, blocks | Thread-safe — only one thread at a time |
transient | Fields | Skipped during object serialization |
volatile | Fields | Always read from main memory — safe across threads |
// static — shared by all instances
class Counter {
static int count = 0;
Counter() { count++; }
}
// final — cannot change
final double PI = 3.14159;
// PI = 3.0; // ERROR
// abstract — must override
abstract class Shape {
abstract double area();
}
class Square extends Shape {
double side;
double area() { return side * side; }
}`private` access modifier என்ன செய்யும்?
- எல்லா classes-லும் access கொடுக்கும்
- Same package-ல் மட்டும் access கொடுக்கும்
- அந்த class-க்குள் மட்டும் access கொடுக்கும்
- Subclasses-க்கு access கொடுக்கும்
Reveal answer / விடை
அந்த class-க்குள் மட்டும் access கொடுக்கும்
`private` — most restrictive. அந்த class-க்குள் மட்டுமே access. Encapsulation implement செய்ய இதை use செய்வோம். Getter/setter methods மூலம் controlled access கொடுக்கலாம்.
முழுமையான உதாரணம்
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 {
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.
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
Try reassigning Settings.LANGUAGE, then undo that change.
Reveal the answer and reasoning / விடை
The assignment fails to compile because the field is final.
தவிர்க்க வேண்டிய தவறு: static and public are different concepts: being static does not make a member accessible everywhere.
பாடத்தின் சுருக்கம்
- 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.