About Question enthuware.ocajp.i.v7.2.1230 :
Moderator: admin
About Question enthuware.ocajp.i.v7.2.1230 :
This question says that, when run, it'll throw an Throwable, but there is no main method to allow this class to run.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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:
HTH,
Paul.
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;
}
}
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1230 :
I just don't get why it throw something. I my mind it can't even been executed
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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.
This is a very common pattern for problem statements even in the exam.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Re: About Question enthuware.ocajp.i.v7.2.1230 :
Good day. I paste this code in IDE IntelliJ IDEA. And result was "Hello".
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
For the purpose of the exam, you should not worry about any IDE but about what the specification says.Guest wrote:Good day. I paste this code in IDE IntelliJ IDEA. And result was "Hello".
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 15
- Joined: Fri Jul 12, 2013 12:34 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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?
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?
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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.
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.
If you like our products and services, please help us by posting your review here.
-
- Posts: 78
- Joined: Sun Jun 30, 2013 10:04 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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.
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.
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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
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
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
You need to rely on command line and not on IDEs.
HTH,
Paul.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 33
- Joined: Wed Nov 13, 2013 4:11 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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
"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
-
- Posts: 15
- Joined: Thu Feb 05, 2015 3:50 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
Ok understood.
-
- Posts: 1
- Joined: Sun Mar 22, 2015 8:18 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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.
"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.
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 5
- Joined: Fri Aug 28, 2015 7:24 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
I just tried and:
Error: Could not find or load main class TestClass
from cmd windows 7
Error: Could not find or load main class TestClass
from cmd windows 7
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
And that is also what the correct option says. It will throw an Error at runtime.
If you like our products and services, please help us by posting your review here.
-
- Posts: 1
- Joined: Tue Aug 02, 2016 8:55 am
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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?"
"What will be the output of the following code snippet?"
"Consider the following method: (...) What will it print?"
-
- Site Admin
- Posts: 10053
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocajp.i.v7.2.1230 :
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."What will be the output of the following code snippet?"
"Consider the following method: (...) What will it print?"
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
Who is online
Users browsing this forum: No registered users and 52 guests