• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Review Q #4 in chapter 1 of OCAJP 8

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
author
Posts: 4335
39
jQuery Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you, Ganish and Scott!
That clears it up.
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is it because the parameter is a String and not a String array?
 
Roel De Nijs
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Eclipse IDE MySQL Database Tomcat Server Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator

    Suehyun Lim wrote:Thank you, Ganish and Scott!

    Your most welcome
     
    What's a year in metric? Do you know this metric stuff tiny ad?
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic