Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Review Q #4 in chapter 1 of OCAJP 8

 
Suehyun Lim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello

Can someone help me?

In the review question #4 in chapter 1 of OCAJP 8, I don't understand why the code compiles. It's a public class, and it doesn't have a main() method. Doesn't that mean that it won't compile?

Thanks.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Coderanch!
Suehyun Lim wrote:Doesn't that mean that it won't compile?
No, it will compile for sure because when you run that program that time an instance of JVM is created for that program (Each application has its own instance of JVM) and its non-daemon thread search for main method with public static void main(String args[]) signature (This is the entry point) in a class which has public access-modifier in file which has same name as that class.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you type javac programname.java here javac is the compiler which compiles .java file into byte code having .class extension. so compiler checks for compile time errors like syntax error etc. and when you type java programname that time JVM starts and looks for main method.
 
Suehyun Lim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So when it says in the book,

"If a main() method isn't present in the class we name with the .java executable, the process will throw an error and terminate,"

it's talking about when it runs and not when it compiles?
 
Scott Selikoff
author
Saloon Keeper
Posts: 4032
18
Eclipse IDE Flex Google Web Toolkit
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Compiled is not the same as Executable.

Compiled means the code uses valid Java syntax and is able to be converted from .java to .class. Executable, on the other hand, means it contains a void main() method with public/static modifiers. All valid Java classes can be compiled, but only those that contain the appropriate main() method can be executed.
 
Suehyun Lim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you, Ganish and Scott!
That clears it up.
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Suehyun Lim,

First of all, a warm welcome to CodeRanch!

Suehyun Lim wrote:In the review question #4 in chapter 1 of OCAJP 8, I don't understand why the code compiles. It's a public class, and it doesn't have a main() method. Doesn't that mean that it won't compile?

The main method is not required to successfully compile the class. So if a class has valid syntax, but no main method, the class will still compile successfully. For example, these classes will all compile successfully (although none of them defines a main method)

If you want to execute the class, you need a main method with the required method declarationSo this class will compile, run and print "Java rocks!"But although this class compiles successfully, it won't run. Can you see why?

Hope it helps!
Kind regards,
Roel
 
Suehyun Lim
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is it because the parameter is a String and not a String array?
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suehyun Lim wrote:Is it because the parameter is a String and not a String array?

Exactly! This method must
  • be named main (case sensitive)
  • be public
  • be static
  • have void as return type
  • have a String array parameter
  • The special main method has to fulfill all these requirements! Otherwise the class will not be executable. But if all syntax is valid, the class will definitely compile.

    Hope it helps!
    Kind regards,
    Roel
     
    Ganish Patil
    Ranch Hand
    Posts: 529
    19
    Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Suehyun Lim wrote:Thank you, Ganish and Scott!
    Your most welcome
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic