• Post Reply Bookmark Topic Watch Topic
  • New Topic

main() is not compiled  RSS feed

 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ??
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I didn't get it. Can you please explain it differently ??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

Henry
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 43
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Shivit Agarwal
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
well then why replacing main function with MAIN is not showing error ??
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 212
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Owen Luo
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot everybody, I got it finally. Actually I interpreted the main concept differently. Sorry for that.

Once again, thank you very much.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!