Page 1 of 1

About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Fri Apr 13, 2012 9:03 am
by Matheus
This question says that, when run, it'll throw an Throwable, but there is no main method to allow this class to run.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Fri Apr 13, 2012 11:09 am
by admin
Do you mean there is no valid main method? Then yes, that is why it will throw an exception when run. This is also explained in detail in the given explanation.

Or do you mean you don't see any method in the given code? Because there is a main method defined in the given code:

Code: Select all

public class TestClass{   
  public static long main(String[] args){
      System.out.println("Hello");
      return 10L;
  }
}
HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Sat Apr 14, 2012 5:52 pm
by Matheus
I just don't get why it throw something. I my mind it can't even been executed

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Sat Apr 14, 2012 7:50 pm
by admin
You are right. It can't be run but when the question says "...when run", it means the question expects you to do: c:/>java TestClass

This is a very common pattern for problem statements even in the exam.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Sun Apr 15, 2012 7:51 pm
by Matheus
Ok, thanks

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Tue Dec 11, 2012 3:23 pm
by Guest
Good day. I paste this code in IDE IntelliJ IDEA. And result was "Hello".

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Tue Dec 11, 2012 7:08 pm
by admin
Guest wrote:Good day. I paste this code in IDE IntelliJ IDEA. And result was "Hello".
For the purpose of the exam, you should not worry about any IDE but about what the specification says.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Wed Nov 06, 2013 10:44 am
by TheSarjo
About this question, i wanted to ask:

public int main(String [] args)

is a valid declaration of a method named main.

BUT

public static int main(String [] args)

is an invalid declaration of THE main method.
Why?

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Wed Nov 06, 2013 11:49 am
by admin
When you say "the" main method, it implies that you are talking about the main method that is used by the JVM to "run" the class from command line.

If you try to "run" a Java class from command line, the JVM expects a public static main method that returns void and takes String[] as the only parameter.

You can have other main methods but the JVM will not accept them for the purpose of running the class from the command line.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Tue Dec 24, 2013 9:49 am
by ewebxml
I selected option e) None of the above
because when I ran the program I received

C:\Java_unique>java TestClass
Error: Main method must return a value of type void in class TestClass, please
define the main method as:
public static void main(String[] args)

-----
On the other hand, Error is subclass of Throwable.
I am assuming that the test question wants you to think along these lines.
Error is a Throwable.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Sun Dec 29, 2013 2:11 pm
by javaman
Hello,
When I try to run this code from NetBeans, I get an alert saying the main() method is missing (as other reported). So I get no compiletime error or runtime error.
At the exam I'd have to guess between 'c' (does not compile) and 'd' (throws error at runtime). But since the correct answer is 'd', do I understand correctly that a class that is missing the main() method compiles correctly but throws an error at runtime?
Thanks

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Tue Dec 31, 2013 9:23 pm
by admin
You need to rely on command line and not on IDEs.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Thu Jan 02, 2014 4:57 am
by javaman
Ok, just for future reference (and for others who might have the same question): if I have a LearnJava.java class, containing 1 public class that contains a method "public static void main1(String args[])" (note: this is not a valid main method) I can sompile it succesfully with "javac LearnJava.java". But running the file gives me
"Error: Main method not found in class LearnJava, please define the main method a
s:
public static void main(String[] args)"
So yes, the compiler allows you to not have a main method and the error will be thrown by the JVM.
Thanks,
Marc

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Thu Feb 19, 2015 4:21 pm
by Venceslas
Ok understood.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Sun Mar 22, 2015 8:30 pm
by dghant1024
I would have understood this question better if i were stated more explicitly see below:

"What will the following code print [when ran from command line] ? "

instead of

" What will the following code print when run?"

public class TestClass{   
public static long main(String[] args){
     System.out.println("Hello");      
return 10L;   }
}

Given the signature of the method above, I assumed that this method was NOT going to executed from command line.

Your insight(s) are welcome.

Thanks.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Mon Mar 23, 2015 4:38 am
by admin
Your point is valid but we have followed the convention used in the exam. By "run" or "execute", it implies running from command line.

HTH,
Paul.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Fri Aug 28, 2015 7:27 am
by danko82
I just tried and:

Error: Could not find or load main class TestClass

from cmd windows 7

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Fri Aug 28, 2015 11:09 am
by admin
And that is also what the correct option says. It will throw an Error at runtime.

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Tue Aug 02, 2016 9:23 am
by MarekM
Hey. Could someone write how the exam is to formulate as regards about calling code from another method? I understand now that the "run" or "execute" means starting from the command line. What if there is a similar question which means calling this code from another method? How do they usually express it? Something like that?:

"What will be the output of the following code snippet?"

"Consider the following method: (...) What will it print?"

Re: About Question enthuware.ocajp.i.v7.2.1230 :

Posted: Tue Aug 02, 2016 9:59 am
by admin
"What will be the output of the following code snippet?"

"Consider the following method: (...) What will it print?"
The exam uses both the styles. In both the cases, it means that the given piece of code or method is executed somehow by the JVM and you are required to determine the result as per the problem statement. Usually it is about the output of print statements on the command line or state of some variable.

HTH,
Paul.