Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

main() is not compiled

 
Ranch Hand
Posts: 82
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ??
 
Rancher
Posts: 43027
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't get it. Can you please explain it differently ??
 
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 43
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well then why replacing main function with MAIN is not showing error ??
 
Henry Wong
author
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 23912
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 212
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic