Nearby lessons
31 of 125Java - Methods
- 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.
Method Details — Signature, Prototype, Mutator, Accessor
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.
Var-Args (Variable Length Arguments)
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.
- 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).