I was wondering what the effect of creating extra main methods would do to your code.
For example,
public class TestClass {
public static void main (String[] args){
TestClass foo = new TestClass();
}
}
After the program initially starts up, foo will be created and it would have another public main method inside it. Will that cause any errors?
The main method is static, which means it belongs to the class rather than the object. So the object won't have another main method inside it at all.
You could call the main method on instances of the object, but if you do that it's literally just another way of calling TestClass.main() (and it's frowned upon by many, including me, to call a static method on an instance of an object anyway.)
If you're referring to multiple main methods in the same program, then this isn't a problem either. The main class is simply specified and its main method is executed to start the program (in the case of a jar file this is the main-class attribute in the manifest file.)
After searching for a Java Class with multiple main() methods or in plain words, overloaded main() methods, I came up with an example of my own. Please have a look
}
I tested this Java Code on JDK 1.7 and works like a charm !
You need "public static void main(String args[])" to start with and then you can call overloaded main methods inside this main and it should work for sure.
Any comments and suggestion are highly appreciated. I am just a novice Java Developer willing to develop my Java skills.
Thanks, PK
Another interesting point to consider is a case where you have two different classes in of java file.
For example, you have Java file with two classes:
Compiling it with
javac FirstClassMultiply.java
will generate two.class
files, first one isFirstClassMultiply.class
and second one isSecondClass.class
And in order to run it you will need to do it for the generated
.class
files:java FirstClassMultiply
andjava SecondClass
, not the original filename file.Please note a couple of additional points:
SecondClass.class
although it's class wasn't public in the original file!FirstClassMultiply
overloading of the main method of is totally fine, but, the only entry point to your prog will be the main method withString args[]
argument.when you run your Java class it will always look for the signature public static void main(String args[]) in the class. So suppose if you invoking by command line argument, it will look for the method Signature in the class and will not invoke other until if u explicitly inoke it by its class name.
But when you try the same from eclipse it will ask for which class to compile. Means MainMethod1 or Mainmethod2. So if te class has the exact signature they can be used as individual entry point to start the application. Coming to your question, If you remove the signature as u did above by changing the argument if main method. It will act as a normal method.
It is all about the execution engine of JVM. Remember, you write
>java com.abc.MainClass
on cmd prompt.It explains everything. If main method is not found here it throws a run time Error:Main Method Not Found in class MainClass. Now if main method is found here, it acts as the first point when Program Counters have to map and start executing the instructions. Referred classes are loaded then, referred methods may be called using the instances created inside. So, main is class specific though one class can have only one main method. Please note, main method's signature never changes. You can have two overloaded main methods in same class, like
During Static binding, the original main is resolved and identified by execution engine.
It will cause no errors. Just because you initialize an object, doesn't mean the main method gets executed. Java will only initially call the main method of the class passed to it, like
>java TestClass
However, doing something like:
Or
That would cause a
StackOverflowError
, because you are explicitly calling TestClass's main method, which will then call the main method again, and again, and again, and....When in doubt, just test it out :-)