Java Class Methods
Ask an object to do something.
Read this topic in தமிழ் ↗.
By the end of this lesson, you can…
- Explain the key idea behind Class Methods.
- Read and run a complete example, then predict what changes.
- Recognize the topic-specific mistake discussed below.
Understand the idea
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.
Explore the details
Java Class Methods
Methods defined inside a class describe the behaviour of its objects. You call them using the dot operator: object.method().
Use the dot operator (.) to access both attributes (obj.brand) and methods (obj.honk()) on an object. Methods with void return type perform an action but return nothing; methods with a type like int give back a value.
Static vs Instance Methods
A static method belongs to the class itself — you call it without creating an object. An instance method belongs to an object and can access its attributes.
class MathHelper {
// Static — no object needed
static int square(int n) {
return n * n;
}
}
class Car {
String brand;
// Instance — belongs to a specific Car object
void describe() {
System.out.println("I am a " + brand);
}
}
// Calling each type
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 |
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.
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.
Make it your own
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.
One mistake to avoid: A static method cannot directly use an instance field without an object reference.
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.