I have recently started learning java and its my first OOP
language.I read that static methods
do not require the class to instantiated they run when you feed the class to the JVM
.My question is what would happen if the static
method
is inside a private nested class
. will it still run?
EDIT- I tried it does not work, I want to know what is happening in the background.
public class tester{
private class estupid{
public static void main(String[] args){
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
To the people down voting, A suggestion,A more productive activity would be telling what's wrong with the snippet, Thank You.
There are many mistakes, which you can simply get by compiling the code. I suggest you to use command line javac compilation
If you compile your code as it is
C:\src>javac tester.java tester.java:3: error: Illegal static declaration in inner class tester.estupid public static void main(String[] args) { ^ modifier 'static' is only allowed in constant variable declarations 1 error
As per above error, make your nested class as static nested class. Now the code will compile successfully but you will get error while running it:
C:\src>javac tester.java C:\src>java tester Error: Main method not found in class tester, please define the main method as: public static void main(String[] args) or a JavaFX application class must extend javafx.application.Application
With above error you can understand that you are running tester class, but it does not contain any main method which is searched by the JVM. So add a main method in tester class and yes you can call the static method in static inner class. Changed code will be like this which will run properly:
public class tester { private static class estupid { public static void main(String[] args) { System.out.println("Hello Im a static method of a private class and main too"); } }}public static void main(String[] args) { estupid.main(args); }
After compiling and running the above code
C:\Himanshu\GitHub\hsingh-learning\src>javac tester.java C:\Himanshu\GitHub\hsingh-learning\src>java tester Hello Im a static method of a private class and main too
This is just to correct your code and making it compilable and runnable, BUT writing main method in nested class is not suggested. Other thing is that you are making private nested class, so you are making it unaccessible from outside of the holding class (tester class in your case). tester class is public and accessible to JVM but the nested class is marked private so that cannot be accessed.
That does not mean you cannot invoke main static method of nested class from JVM. Make your nested class public.
public class tester {
public static class estupid {
public static void main(String[] args) {
System.out.println("Hello Im a static method of a private class and main too");
}
}
}
Compile it, which will produce 2 class files. 1. tester.class 2. tester$estupid.class
Run the 2nd tester$estupid which contains the main method (which is required by JVM)
C:\Himanshu\GitHub\hsingh-learning\src>java tester$estupid
Hello Im a static method of a private class and main too