Java Class Methods

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

Java Class Methods

Ask an object to do something.

Step-by-step explanationsRunnable JavaPractice + answer

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.
01 / The concept

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.

02 / Step by step

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.

Java reference fragmentJAVA
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
FeatureStatic MethodInstance Method
Called onClass name: MathHelper.square()Object: c.describe()
Access attributes?No (no this)Yes (has this)
Keywordstatic(no keyword needed)
Best forUtility / helper logicObject behaviour
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 {
        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.

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

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.

Coding Tamilan · Understand the idea. Try the code. Build the habit.