When the compilation unit gets compiled by Java compiler, no. of files gets created with each class name as the individual file as name.class.
Likewise a file containing main() is created say mainclass.class which we later pass to JVM for output. But in reality class with main() never gets compiled. Then how come we get mainclass.class
Eg- class halo{...} // for this halo.class file is created after compilation. class menu{...} // menu.class
class mainmenu { public static void main(....) {.......... } } Though the class mainmenu doesn't get compiled but still mainmenu.class is created why ?? How it is created actually ??
Have the determination of mirror which never fails to reflect in spite of being broken into pieces.<br /> <br />Kiss the hands you cannot bite.<br /> <br />An Optimist is one who starts taking a bath when he accidentally falls into the water.
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35224
7
posted
0
The class is probably used/accessed from one of the other classes that you are compiling. The Java compiler will compile all classes that it needs for compiling the classes you specified, and to which it can find the source code.
Originally posted by Shivit Agarwal: I didn't get it. Can you please explain it differently ??
To repeat what Ulf said in a different way... when the compiler compiles your class, it will also check to see if the classes which are used by your class is also is compiled. If not, it will compile those classes too.
You didn't get me or I didn't get you. I know all other classes will get compiled but howcome main.class is formed cause it doesn't get compiled. Why ??
If we replace "main" keyword by MAIN the program gets compiled easily(without error). Thats suggest class containing main() is not compiled and all other classes are. For all other classes java compiler creates an individual file for each class name with (class name identifier).class.
But if we see it also creates the (main class identifier).class but in reality class containing main() is not comipled.
Krep Lock
Ranch Hand
Joined: Sep 19, 2006
Posts: 43
posted
0
if mainmenu.class is created, how are you sure the mainmenu.java is not being compiled? what do your javac and java commands look like, and what is the output?
Owen Luo
Greenhorn
Joined: Mar 19, 2008
Posts: 5
posted
0
Originally posted by Shivit Agarwal: [QB]Likewise a file containing main() is created say mainclass.class which we later pass to JVM for output. But in reality class with main() never gets compiled. Then how come we get mainclass.class [QB]
I don't know why you think a class with main() will never gets compiled.
I am a fresh guy in JAVA with pool english.
Shivit Agarwal
Ranch Hand
Joined: Feb 28, 2008
Posts: 82
posted
0
well then why replacing main function with MAIN is not showing error ??
Originally posted by Shivit Agarwal: well then why replacing main function with MAIN is not showing error ??
What does this even mean?!?!?
Can you post the code which you think should be an error? And can you post why this should be an error?
Henry [ March 23, 2008: Message edited by: Henry Wong ]
Shivit Agarwal
Ranch Hand
Joined: Feb 28, 2008
Posts: 82
posted
0
Say,
Class XYZ {....}
Class ABC { public static void MAIN(String[] args) { ............} }
See here the MAIN is in Uppercase not main. Since Java is case sensitive the program should flag an error but it doesn't. This mean class ABC doesn't get compiled. And two files are formed - 1. XYZ.class(this is okay as it is compiled) but 2. ABC.class if this class doesn't get compiled then how come ABC.class is formed.
2. ABC.class if this class doesn't get compiled then how come ABC.class is formed.
The ABC class got compiled. This is why you have a ABC.class file. Of course, this ABC class doesn't have a main() method, just like your XYZ class doesn't have a main() method.
Your ABC class has a MAIN() method though, which is just another method in the class.
Henry
David McCombs
Ranch Hand
Joined: Oct 17, 2006
Posts: 212
posted
0
Originally posted by Shivit Agarwal: Say,
Class XYZ {....}
Class ABC { public static void MAIN(String[] args) { ............} }
See here the MAIN is in Uppercase not main. Since Java is case sensitive the program should flag an error but it doesn't. This mean class ABC doesn't get compiled. And two files are formed - 1. XYZ.class(this is okay as it is compiled) but 2. ABC.class if this class doesn't get compiled then how come ABC.class is formed.
A class doesn't need a main method to be compiled, like was already said it is just another method.
Try to start execution with the class that has MAIN. That is where the error will occur.
"Should array indices start at 0 or 1? My compromise of 0.5 was rejected without, I thought, proper consideration."- Stan Kelly-Bootle
Owen Luo
Greenhorn
Joined: Mar 19, 2008
Posts: 5
posted
0
Originally posted by Shivit Agarwal: well then why replacing main function with MAIN is not showing error ??
I think you have got a wrong concept. a main() method is not a MUST HAVE method to a class. When you rename main() to MAIN(), you created a method named MAIN(). As Henry said before, this behavior won't cause a compile time error.
The class mainmenu will be compiled whether there is a main() or MAIN() method in it. And without a main() method, you can not run into it by "java mainmenu".
Shivit Agarwal
Ranch Hand
Joined: Feb 28, 2008
Posts: 82
posted
0
Thanks a lot everybody, I got it finally. Actually I interpreted the main concept differently. Sorry for that.