Method Invocation in Java
When calling methods within the same class in Java, the class name prefix can be omitted.
/*
Method invocation demonstration
*/
public class MethodDemo03{
public static void main(String[] args){
// Full method call
MethodDemo03.executeMethod();
// Simplified call due to static modifier
executeMethod();
// Calling method from another class
Helper.performAction();
// Direct call attempt - compilation error
// performAction();
}
public static void executeMethod(){
System.out.println("executeMethod running !");
secondaryMethod(); // Works within same class
}
public static void secondaryMethod(){
System.out.println("secondaryMethod running !");
}
}
class Helper{
public static void performAction(){
System.out.println("Helper's performAction invoked !");
// secondaryMethod(); Error - method not accessible
}
}
Program Execution Flow
- Method code executes sequential from top to bottom
- Subsequent lines cannot execute untill current operation completes
public class MethodDemo04{
public static void main(String[] args){
System.out.println("main start");
firstMethod();
System.out.println("main end");
}
public static void firstMethod(){
System.out.println("firstMethod start");
secondMethod();
System.out.println("firstMethod end");
}
public static void secondMethod(){
System.out.println("secondMethod start");
System.out.println("secondMethod end");
}
}
/*
Execution sequence:
main start
firstMethod start
secondMethod start
secondMethod end
firstMethod end
main end
main() starts first and ends last
secondMethod() ends first among all methods
*/
Understanding Return Statements
- Execution of return immediately terminates the containing method
- No code can follow a return statement within the same scope
public class MethodDemo05{
public static void main(String[] args){
displayGreeting();
processValue();
}
/*
public static int checkValue(int input){
if (input > 10){
return 1;
}
}
Compilation error: missing return statement
Compiler cannot guarantee execution of "return 1;"
*/
public static int validateNumber(){
int value = 10;
if (value > 10){
return 1;
}else{
return 0;
}
}
public static int processValue(){
int value = 10;
if (value > 10){
return 1;
// System.out.println("Message"); Compilation error
}
System.out.println("Message");
return 0;
}
public static void displayGreeting(){
System.out.println("Greetings")
}
}
- In methods with void return type, use
return;statement
public class MethodDemo06{
public static void main(String[] args){
iterateNumbers();
}
public static void iterateNumbers(){
for(int counter = 0; counter < 10; counter++){
if(counter == 5){
return ; // Terminates entire method, not just loop
// Unlike break; which only exits the loop
}
System.out.println("counter --> " + counter);
}
System.out.println("Loop completed!");
}
}
JVM Memory Allocation
- Methods are defined but not executed until caled; no memory allocated in JVM beforehand
- JVM has three primary memory areas:
- Method area
- Heap memory
- Stack memory
Stack Data Structure
- Stack is a fundamental data structure concept
- Data structures define storage patterns independent of programming languages
- Essential computer science topics: Data Structures + Algorithms
- Common structures: arrays, queues, stacks, binary trees, hash tables, etc.
Stack Operations
Key components: top element, bottom element, stack frame
- Stack pointer always references the top element
- Top element is active; others remain dormant
- Operations: push (insert), pop (remove)
- Storage principle: Last In, First Out (LIFO)
Method Storage and Execution
- Method bytecode resides in method area after class loading
- Method area contains executable code segments
- Each method call allocates separate runtime space in stack memory
- Call process involves stack operations:
Push operation: Allocates method memory
Pop operation: Releases method memory upon completion
- Local variables declared within methods are stored in stack memory during execution
- This explains why local variables have short lifecycles - they're destroyed when method completes and stack frame pops
- Previous execution example can now be understood through stack behavior