Java Class Methods
Ask an object to do something.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- Explain the key idea behind Class Methods.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
Instance methods operate in the context of an object and can read its fields. Static methods have no implicit current object. Put behavior with the state it acts on, and choose static for operations that do not need instance state.
விரிவான விளக்கங்கள்
Java Class Methods
Methods-ஐ class-க்கு உள்ளே declare பண்றோம் — object அல்லது class-ஐ use பண்ணி call பண்ணலாம்.
static methods object இல்லாமலே call ஆகும். public methods object-ஐ use பண்ணி call பண்றோம்:
Static vs Instance Methods
class MathHelper {
static int square(int n) { return n * n; } // Static
}
class Car {
String brand;
void describe() { System.out.println("I am a " + brand); } // Instance
}
System.out.println(MathHelper.square(4)); // 16 — no object
Car c = new Car();
c.brand = "Toyota";
c.describe(); // I am a Toyota| Feature | Static Method | Instance Method |
|---|---|---|
| Called on | Class name: MathHelper.square() | Object: c.describe() |
| Access attributes? | No (no this) | Yes (has this) |
| Keyword | static | (no keyword needed) |
| Best for | Utility / helper logic | Object behaviour |
`static` method-ஐ எப்படி call செய்வோம்?
- Object create செய்து call செய்வோம்
- ClassName.methodName() என்று directly call செய்வோம்
- super.methodName() என்று call செய்வோம்
- this.methodName() என்று call செய்வோம்
Reveal answer / விடை
ClassName.methodName() என்று directly call செய்வோம்
`static` methods class-க்கே சொந்தம். Object create செய்யாமல் `Math.sqrt()`, `Arrays.sort()` போல் ClassName.method() என்று call செய்யலாம்.
முழுமையான உதாரணம்
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 {
Greeter greeter = new Greeter();
greeter.name = "Kavi";
greeter.greet();
System.out.println(Greeter.doubleValue(6));
}
}
class Greeter {
String name;
void greet() { System.out.println("Hello, " + name); }
static int doubleValue(int n) { return n * 2; }
}Predict first. Then reveal the expected output / வெளியீடு
Hello, Kavi 12
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
Create a second greeter with another name and call greet on it.
Reveal the answer and reasoning / விடை
Each call uses the name stored in the object on which greet is invoked.
தவிர்க்க வேண்டிய தவறு: A static method cannot directly use an instance field without an object reference.
பாடத்தின் சுருக்கம்
- 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.