Methods of Java · Introduction to Java Programming

Instance Methods

Watch lectures

Instance Methods

1. What They Are (beginner)

Instance methods = work on one object's data (s.length(), a.deposit(100)). Need object (new) to call. Can use fields + this + other instance/static methods.

2. Code — Bank Account (classic!)

class Account {
    private double balance; // per-object!
    void deposit(double x) { // instance (no static!)
        if (x > 0) balance += x; // uses field!
    }
    double getBalance() { return balance; }
    void show() { System.out.println("bal=" + this.balance); }
}
Account a = new Account(); // object needed!
a.deposit(100);            // instance call (dot!)
System.out.println(a.getBalance()); // 100
// Account.deposit(50);    // ERROR! (no object!)

3. Instance vs Static (exam table!)

Instance Static
Call obj.m() (needs new!) Class.m() (no object!)
Uses fields + this static only (no this!)
Example s.length() Math.sqrt(16)

4. Traps

  • static method calling instance method directly = ERROR (make object first!).
  • Instance method CAN call static (fine — class-level always there!).

5. Exam Points

  • Account program + table — 5 marks, very frequent.
  • Call-style errors — 3 marks, frequent.
Free AppAndroid App

Coursify Android App

Generate AI courses, take quick quizzes between classes, and master complex engineering concepts on the go.

← All Introduction to Java Programming modules