A method is a named block of code that does one job. Methods let you write a task once and reuse it, breaking a big program into small, testable pieces. This is a core ICSE Class 10 topic.
1Why methods — structure, prototype & signature
Methods avoid repeating code and make programs readable. A method's general form:
returnType name(parameter list) {
// body
return value; // if returnType is not void
}
int add(int a, int b) { // returns an int
return a + b;
}| Term | Means |
|---|---|
| Prototype | The method's heading: return type + name + parameter types |
| Signature | The method name + its parameter list (types & order) |
| return | Sends a value back; a void method returns nothing |
- A method is a named, reusable block of code that does one job.
- Form: returnType name(parameters) { body }; void means it returns nothing.
- Signature = method name + parameter list; prototype = the method's heading.
2Method overloading
Overloading means writing several methods with the same name but different parameter lists (different number or types of parameters). Java picks the right one from the arguments you pass.
int add(int a, int b) { return a + b; }
double add(double a, double b) { return a + b; }
int add(int a, int b, int c) { return a + b + c; }
add(2, 3); // calls the first
add(2.5, 1.0); // calls the second
add(1, 2, 3); // calls the third- Overloading = same method name, different parameter lists (count or types).
- Java chooses which overloaded method to run from the arguments passed.
- Differing only by return type is NOT valid overloading.
3Parameters: actual vs formal; value vs reference
When you call a method you pass actual parameters (arguments); the method receives them as formal parameters:
int square(int n) { // n is the FORMAL parameter
return n * n;
}
square(5); // 5 is the ACTUAL parameterPass by value vs pass by reference
| Pass by value | Pass by reference | |
|---|---|---|
| What is passed | A copy of the value | The location of the original |
| Changes affect original? | No | Yes |
| In Java | Primitives (int, double…) | Objects/arrays (the reference is passed) |
- Actual parameters are the values you pass; formal parameters receive them in the method.
- Pass by value passes a copy (original unchanged) — Java does this for primitives.
- Pass by reference passes the location (changes affect the original) — Java passes object/array references this way.
4static vs non-static; pure vs impure
| static method | non-static method | |
|---|---|---|
| Belongs to | The class itself | An object of the class |
| Called as | ClassName.method() | object.method() |
| Example | Math.sqrt(9) | s.display() |
Pure vs impure methods
- A pure method does not change the object's state (member variables) — it only reads/returns. E.g. a method that returns the area from existing data.
- An impure method changes the object's state (modifies member variables).
int getMarks() { return marks; } // pure (only reads)
void setMarks(int m) { marks = m; } // impure (changes state)
- A static method belongs to the class (Math.sqrt); a non-static method belongs to an object.
- A pure method does not change the object's state (only reads/returns).
- An impure method changes the object's state (modifies member variables).
★ Practical: write some methods
In BlueJ, write methods that:
- A method int max(int a, int b) that returns the larger value.
- Overload it as double max(double a, double b).
- A pure method that returns a stored value and an impure method that changes it.
- Identify the actual and formal parameters in one of your calls.
Ready for the chapter test?
Answer 5 questions. Score 60% or more to mark this chapter complete.
Start the test →💡 Log in to save your progress and earn the certificate.