• 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

Does the answer to this Java question about main methods make sense? (Q1-7, Mala Gupta)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I am studying for the Java 7 SE Programmer I exam and I was going through practice questions. I don't seem to understand one of the options though. Here's the question:
Select the correct options:
  • You can start the execution of a Java application through the main method.
  • The Java compiler calls and executes the main method.
  • The Java Virtual Machine calls and executes the main method.
  • A class calls and executes the main method.

  • the correct answers are the 1st and 3rd option. I understand the 3rd option. I don't get the first option, it's worded so weirdly. Could someone explain it to me?
    Thanks
     
    Ranch Hand
    Posts: 234
    12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    One of the first things you learn in Java programming is how to run a Java program. You provide a class that has a main() method with a specific signature and then you use the java command to run the Java program.
     
    Rancher
    Posts: 1093
    29
    Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This is how I read it: If you make a new thread and make the call: MyObject.main(String), you will have your application running in that thread. Main is the entry point to your application.

    Even though, you have a main() in your Class, you can still call the constructor of that Class and it will happily instantiate you an instance of that class, provided it's not static, and your app will not start. You have to call main to start your app. As convention, Java VM does that for you, or at least tries to do that for you when you tell the VM to run your app.

    あ Ali wrote:Hi,
    I am studying for the Java 7 SE Programmer I exam and I was going through practice questions. I don't seem to understand one of the options though. Here's the question:
    Select the correct options:

  • You can start the execution of a Java application through the main method.
  • The Java compiler calls and executes the main method.
  • The Java Virtual Machine calls and executes the main method.
  • A class calls and executes the main method.

  • the correct answers are the 1st and 3rd option. I understand the 3rd option. I don't get the first option, it's worded so weirdly. Could someone explain it to me?
    Thanks

     
    あ Ali
    Greenhorn
    Posts: 2
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    thanks
     
    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,

    First of all, a warm welcome to CodeRanch!

    I don't get the first option, it's worded so weirdly. Could someone explain it to me?


    If you want to compile a Java source code file (using javac), you can compile any class you want, even an empty one, like this oneBut if you want to run a Java class (using java), the class needs to have a main() method. And this method must have a very specific declarationOnly classes with such a main() method can be executed using java. If the class has no main() method or if the declaration of the main() method differs from the very specific (expected) declaration will throw an exception (error) at runtime when this class is being executed using java. Let's start with a class with a valid main() methodWhen you execute the RunMe class, the output will be "Executing... Done!".

    Although the following declaration of the main() method looks very similar to the expected declaration, it has a very slight difference and therefore this class is not eligible to be executed using javaWhen you try to execute the CanNotRunMe class, you'll get the following error at runtime (so it is not a compiler error):
    Error: Main method not found in class CanNotRunMe, please define the main method as:
    public static void main(String[] args)


    Hope it helps!
    Kind regards,
    Roel

    PS. On CodeRanch, when you copy questions from a book, mock exam or other source, you are required to QuoteYourSources. Because this is your first post here, I updated the subject and added the source for you!
     
    Greenhorn
    Posts: 11
    Android Spring Java
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    He has a fair point, the question is indeed poorly worded.

    It could be seen as asking if you can run the application by directly invoking the main method, rather than allowing the JVM to do it.
    It is true the JVM implicitly calls the main method, but statically calling main, will not work.
     
    He got surgery to replace his foot with a pig. He said it was because of this tiny ad:
    a bit of art, as a gift, the permaculture playing cards
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic