How to Call a Method From Another Class in Java
In Java, Can we call the main() method of a class from another class?
In Java, Can we call the main() method of a class from another class?
OR
How to call 'public static void main(String[] args)' method from our code?
These are some questions that usually puzzles a Java programmer. This article aims to provide an answer to these problems in a simple and efficient way.
Attention reader! Don't stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course .
As we know, the main() method for any Java application as the Java Run time environment calls the main() method first. So it is obvious that we don't need to call the main() method by ourselves as it is already called when the program starts. But what if we want to call the main() method from somewhere in our program? That's the problem.
Solution:
Though Java doesn't prefer main() method called from somewhere else in the program, it does not prohibit one from doing it as well. So, in fact, we can call the main() method whenever and wherever we need to.
But calling the main() method from our code is tricky. It can lead to many errors and exceptions, such as:
- The main() method must be called from a static method only inside the same class.
import
java.io.*;
class
GFG {
void
mainCaller()
{
System.out.println(
"mainCaller!"
);
main(
null
);
}
public
static
void
main(String[] args)
{
System.out.println(
"main"
);
mainCaller();
}
}
Compilation Error in java code:
prog.java:27: error: non-static method mainCaller() cannot be referenced from a static context mainCaller(); ^ 1 error
- The main() method must be passed the String[] args while calling it from somewhere else.
import
java.io.*;
class
GFG {
static
void
mainCaller()
{
System.out.println(
"mainCaller!"
);
main();
}
public
static
void
main(String[] args)
{
System.out.println(
"main"
);
mainCaller();
}
}
Compilation Error in java code:
prog.java:17: error: method main in class GFG cannot be applied to given types; main(); ^ required: String[] found: no arguments reason: actual and formal argument lists differ in length 1 error
- Calling the main() method will lead to an infinite loop as the memory stack knows to run only the main() method.
import
java.io.*;
class
GFG {
static
void
mainCaller()
{
System.out.println(
"mainCaller!"
);
main(
null
);
}
public
static
void
main(String[] args)
{
System.out.println(
"main"
);
mainCaller();
}
}
RunTime Error in java code:
RunTime Error in java code :- Exception in thread "main" java.lang.StackOverflowError mainCaller! main mainCaller! main mainCaller! main . . .
The right way to this:
Example 1: Calling main() method externally from the same class
import
java.io.*;
class
GFG {
static
int
count =
0
;
static
void
mainCaller()
{
System.out.println(
"mainCaller!"
);
count++;
if
(count <
3
) {
main(
null
);
}
}
public
static
void
main(String[] args)
{
System.out.println(
"main"
);
mainCaller();
}
}
Output:
main mainCaller! main mainCaller! main mainCaller!
Example 1: Calling main() method externally from another class
import
java.io.*;
class
GFG {
static
int
count =
0
;
static
void
mainCaller()
{
System.out.println(
"mainCaller!"
);
count++;
if
(count <
3
) {
Test.main(
null
);
}
}
}
class
Test {
public
static
void
main(String[] args)
{
System.out.println(
"main"
);
GFG.mainCaller();
}
}
Output:
main mainCaller! main mainCaller! main mainCaller!
How to Call a Method From Another Class in Java
Source: https://www.geeksforgeeks.org/in-java-can-we-call-the-main-method-of-a-class-from-another-class/
0 Response to "How to Call a Method From Another Class in Java"
Post a Comment