Summary
1. Accessing Members
- You can access both instance and static members from instance methods.
- You can access only static members from static methods.
2. Return Type = void
- You should not specify return value
- You can use empty return statement
return;
3. Return Type ≠ void
- You must return a value
- You cannot use empty return
return value;
4. Return Value Rule
- Returned value must be:
- Same type OR
- Compatible type with return type
5. Arguments Matching Rules
Actual arguments and Formal arguments must match:
- a) Number of arguments
- b) Type (same or compatible)
- c) Order of arguments
📌 Example: Method Overloading
class Hello {
void m1(byte b) {}
void m1(short s) {}
void m1(int i) {}
void m1(long l) {}
void m1(float f) {}
void m1(double d) {}
}
Hello h = new Hello();
🔍 Case Analysis
Case 1: All methods present
byte b=12;
h.m1(b);
➡️ Calls:
m1(byte b)
Case 2: Remove m1(byte)
byte b=12;
h.m1(b);
➡️ Calls:
m1(short s)
Case 3: Remove m1(byte) and m1(short)
byte b=12;
h.m1(b);
➡️ Calls:
m1(int i)
🧠 Key Concept
👉 Java follows type promotion order:
byte → short → int → long → float → double
✔ If exact match not found → next compatible type is chosen
Quick Takeaways
- Instance method → access everything
- Static method → access only static directly
- Void → no return value
- Non-void → must return value
- Overloading → depends on type matching & promotion
Case Continuation (Method Overloading)
Case 4
Hello class contains three methods
(remove m1(byte), m1(short), m1(int))
byte b=12;
h.m1(b);
➡️ Invokes:
m1(long a)
Case 5
Hello class contains two methods
(remove m1(byte), m1(short), m1(int), m1(long))
byte b=12;
h.m1(b);
➡️ Invokes:
m1(float f)
Case 6
Hello class contains one method
(remove m1(byte), m1(short), m1(int), m1(long), m1(float))
byte b=12;
h.m1(b);
➡️ Invokes:
m1(double d)
Case 7
Hello class contains no methods
byte b=12;
h.m1(b);
➡️ Compilation Error
VAR-ARGS (Variable Arguments)
8. Definition
To define a Var-Args parameter, use ellipsis (...) after data type:
void sum(int...values)
9. Internal Working
Var-args are converted into an array by the compiler:
void sum(int...values) →void sum(int[]values)
10. Method Calling
When calling var-args method, Java converts arguments into array:
sum(); →sum(new int[0]);
sum(99,88); →sum(new int[]{99,88});
sum(99,88,77); →sum(new int[]{99,88,77});
11. Usage Rule
Var-args can be used only for method or constructor parameters
Cannot be used for:
- Instance variables
- Static variables
- Local variables
12. Var-Args = Array
Var-args behaves like an array, so you can access it using loop:
void sum(int... values) {
for (int i = 0; i < values.length; i++) {
System.out.println(values[i]);
}
}
Key Takeaways
- Java follows type promotion hierarchy:
byte → short → int → long → float → double
- Var-args:
- Internally array
- Accepts zero or more arguments
- Simplifies method overloading
VAR-ARGS (Continued)
13. Passing values or array
You can pass individual values or an array object to a method having var-args.
sum(10,20);
int arr[]=new int[]{10,20};
sum(arr);
14. Rule for Var-Args
- Only one var-args parameter is allowed in a method.
- It must be the last parameter.
15. Priority Rule
- Fixed argument method has higher priority than var-args method.
Running Java Application
Example:
java Hello
Before Java 7
- Loads the class
- Checks whether
main()method with standard signature is available - If available → invokes
main() - If not available → JVM throws error:
java.lang.NoSuchMethodError: main
Java 7 Onwards
- Checks whether
main()method with standard signature is available - If available → loads class and invokes
main() - If not available → JVM throws error:
Main method not found in class Hello, please define the main method as:
public static void main(String[] args)
Main Method Rules
17. Var-Args in main()
You can use var-args in main method:
public static void main(String[] args)
18. Allowed Modifiers for main()
You can use:
publicstaticfinalstrictfpsynchronized
19. Not Allowed Modifiers for main()
abstractnativeprivateprotected
20. Calling main() explicitly
You can call main() method like a normal method:
main(new String[]{"A","B"});
Key Takeaways
- Var-args must be last parameter
- Only one var-args allowed
- Fixed arguments > Var-args (priority)
- JVM always looks for:
public static void main(String[] args)
String... argsis also valid