Nearby lessons

31 of 125

Java - Methods

📌 What You Will Learn
  • What class and object mean, with real-life examples
  • The four pillars: Encapsulation, Abstraction, Inheritance, Polymorphism
  • Constructors, this, super, static and final
  • Method overloading and method overriding (with a comparison table)
  • Abstract classes and interfaces (including the modern changes)
  • Association, Aggregation and Composition

Methods is a core concept of the Java language. This lesson explains Method Details — Signature, Prototype, Mutator, Accessor, Var-Args (Variable Length Arguments) and The static Keyword with complete, runnable code examples, clear step-by-step explanations, and common mistakes to avoid with exam-style MCQs at the end.

Method Details — Signature, Prototype, Mutator, Accessor

Method Signature vs Method Prototype

Method signature = method name + parameter list. Method prototype = signature + return type (the full declaration line).

Mutator Methods vs Accessor Methods

Mutator (setter) methods change the values of instance variables. Accessor (getter) methods read them.

Example01
JCode Cell
1int add(int a, int b);
2| | | | |
3| | +----+----+-> parameter list
4| +-- method name
5+-- return type
6 
7signature = add(int, int)
8prototype = int add(int, int);

Method Details — Signature, Prototype, Mutator, Accessor

Example02
JCode Cell
1class Student {
2 private int rollNo;
3 
4 void setRollNo(int r) { rollNo = r; } // mutator - sets value
5 int getRollNo() { return rollNo; } // accessor - gets value
6}

Var-Args (Variable Length Arguments)

Var-args lets a method accept any number of arguments (including zero). Java 5 introduced it. Inside the method, the arguments arrive as an array.

Example03
JCode Cell
1void methodName(int... numbers) { // three dots = var-args
2 // numbers is an int[]
3}

Var-Args (Variable Length Arguments)

Trainer's Note: Rules: there can be only one var-args in a method, and it must be the last parameter. Example: void show(String name, int... marks) is correct; void show(int... a, String b) is wrong.
Example04
JCode Cell
1class VarArgDemo {
2 public static void main(String[] args) {
3 add(1, 2);
4 add(1, 2, 3, 4, 5);
5 add(); // zero arguments also OK
6 }
7 static void add(int... nums) {
8 int sum = 0;
9 for (int n : nums) sum = sum + n;
10 System.out.println("Sum = " + sum);
11 }
12}
Output
Sum = 3 Sum = 15 Sum = 0

The static Keyword

static means the member belongs to the class, not to any one object. Static members are shared by all objects and can be called using the class name directly.

Trainer's Note: A static method cannot access non-static (instance) variables directly, because it may run when no object exists. But an instance method can easily access static variables. Also, static blocks run once when the class is loaded — used for one-time setup.
In simple words: A static member belongs to the class, not to any one object. All objects share one copy, so changing it once changes it for every object — and you call it with the class name.
Example05
JCode Cell
1class College {
2 String name; // instance: each object has its own
3 static String city = "Hyderabad"; // static: shared by all objects
4 
5 void display() {
6 System.out.println(name + " - " + city);
7 }
8 static void welcome() { // static method
9 System.out.println("Welcome to college!");
10 }
11}
12 
13class Test {
14 public static void main(String[] args) {
15 College c1 = new College(); c1.name = "CBIT";
16 College c2 = new College(); c2.name = "VNR";
17 c1.display();
18 c2.display();
19 College.city = "Pune"; // change once, changes for all
20 c1.display();
21 c2.display();
22 College.welcome(); // call static method by class name
23 }
24}
Output
CBIT - Hyderabad VNR - Hyderabad CBIT - Pune VNR - Pune Welcome to college!
📝 Key Takeaways
  • Class = blueprint; Object = real thing made from it (memory in heap).
  • Four pillars: Encapsulation (hide data), Abstraction (show only essentials), Inheritance (reuse parent code), Polymorphism (one name, many forms).
  • Overloading = same class, different parameters, decided at compile time. Overriding = child redefines parent method, decided at run time.
  • Constructor initialises the object; name = class name, no return type.
  • this = current object; super = parent class; static = belongs to class; final = cannot change.
  • abstract class cannot be instantiated; interface is a contract a class implements.
  • Association (weak), Aggregation (has-a, part independent), Composition (strong has-a, part dies with whole).

🧠 Test Your Knowledge

1 Questions
Progress: 0 / 1