Parameters, Overloading & Scope
Parameters are variables in the method signature. Arguments are the actual values you pass when calling.
Parameters — passing data into methods
Parameters are variables in the method signature. Arguments are the actual values you pass when calling.
Java passes primitives by value — the method gets a copy. Objects pass reference copy.
Real-life example: Parameters are order details on a food app — you tell the restaurant size (param), they make that size (argument).
static double areaRectangle(double width, double height) {
return width * height;
}
static void printLine(String text, int times) {
for (int i = 0; i < times; i++) {
System.out.println(text);
}
}Method Overloading — same name, different params
You can have multiple methods with the same name if parameter lists differ (count or types).
Compiler picks the right one based on arguments you pass.
Real-life example: "print" can mean print photo or print document — same word, different inputs, different action.
static int add(int a, int b) {
return a + b;
}
static double add(double a, double b) {
return a + b;
}
static int add(int a, int b, int c) {
return a + b + c;
}Scope — where variables live
Local variables exist only inside the block { } where declared. Method params are local to that method.
Instance variables belong to the object. Static variables belong to the class.
Real-life example: A name tag at a party (local) is thrown away when you leave the room. Your ID card (instance) stays with you everywhere.
class Demo {
int instanceVar = 10;
void test() {
int localVar = 5;
System.out.println(instanceVar + localVar);
}
// localVar not visible here
}