Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
あ Ali
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
     
    Daniel Cox
    Ranch Hand
    Posts: 147
    7
    • Mark post as helpful
    • send pies
    • 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.
     
    Les Morgan
    Rancher
    Posts: 710
    17
    C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
    • Mark post as helpful
    • send pies
    • 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
    • Quote
    • Report post to moderator
    thanks
     
    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,

    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!
     
    Stephen M Davies
    Greenhorn
    Posts: 11
    Android Java Spring
    • Mark post as helpful
    • send pies
    • 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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic