About Question enthuware.ocpjp.v7.2.1092 :
Moderators: Site Manager, fjwalraven
-
- Posts: 5
- Joined: Wed Mar 27, 2013 4:14 am
- Contact:
About Question enthuware.ocpjp.v7.2.1092 :
The class MyTread is not public, this programm will not compile. A am right?
-
- Site Admin
- Posts: 10401
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
No. It will compile fine. There is no requirement that every class must be public. You are preparing for OCPJP 7, this is covered in OCAJP itself 
HTH,
Paul.

HTH,
Paul.
-
- Posts: 132
- Joined: Thu May 16, 2013 9:23 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
Hi,
Sure it's not an error? I tried it out and I got the following:
Thanks in advance.
The_Nick.
Sure it's not an error? I tried it out and I got the following:
Code: Select all
Error: Main method not found in class MyTrhead, please define the main method as:
The_Nick.
-
- Site Admin
- Posts: 10401
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
Did you read the explanation?
-
- Posts: 9
- Joined: Sun Aug 25, 2013 2:36 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
Lovely question 

-
- Posts: 17
- Joined: Wed Jan 22, 2014 12:35 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
I think the issue The_Nick has is with the statement:
"an exception will be thrown at runtime"
When run in Eclipse it looks like Eclipse will catch the problem right away and print out
"Error: Main method not found in class MyThread, please define the main method as:.."
When run from the command line the message is:
"Exception in thread "main" java.lang.NoClassDefFoundError:..."
So I guess its OK to get away with calling it an exception but the problem is of type "Error".
"an exception will be thrown at runtime"
When run in Eclipse it looks like Eclipse will catch the problem right away and print out
"Error: Main method not found in class MyThread, please define the main method as:.."
When run from the command line the message is:
"Exception in thread "main" java.lang.NoClassDefFoundError:..."
So I guess its OK to get away with calling it an exception but the problem is of type "Error".
-
- Posts: 77
- Joined: Sun Jun 30, 2013 10:04 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
The following code
prints
"Hello World"
in an infinite loop.
Question: How must the code be written so that an exception is thrown?
Code: Select all
class MyThread extends Thread
{
int i = 0;
public void run()
{
while(true)
{
if( i%2 == 0 )
System.out.println("Hello World");
}
}
public static void main(String args[]){
MyThread mt1 = new MyThread();
mt1.run();
System.out.println();
}
}
"Hello World"
in an infinite loop.
Question: How must the code be written so that an exception is thrown?
-
- Site Admin
- Posts: 10401
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
The code given in the question does not have main method and so when you try to run it from command line, you will get an exception.
HTH,
Paul.
HTH,
Paul.
-
- Posts: 38
- Joined: Fri Nov 13, 2015 4:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
I think the explanation should mention that the exception is thrown by the JVM before the program even starts.
"Make sure that your read the question properly" does not clue you in on this, so the explanation isn't as helpful as those for other questions.
Maybe something like this (changed text in italics):
"Note that there is no problem as such with the program. So there is no compiler error.
But also note that there is no standard main() method. So if you try to run this program from the command line, the JVM will report an error, typically in the form of a MethodNotFoundException."
Note that actually there is not even an exception, at least not with OpenJDK.
It will simply print an error message (no stack trace) and exit:
I am seeing this message regardless of whether the class is public or package-private.
Actually a package-private class is accepted as main class, not sure whether that is spec-conformant or not - hmmm...
"Make sure that your read the question properly" does not clue you in on this, so the explanation isn't as helpful as those for other questions.
Maybe something like this (changed text in italics):
"Note that there is no problem as such with the program. So there is no compiler error.
But also note that there is no standard main() method. So if you try to run this program from the command line, the JVM will report an error, typically in the form of a MethodNotFoundException."
Note that actually there is not even an exception, at least not with OpenJDK.
It will simply print an error message (no stack trace) and exit:
Code: Select all
$ java Test
Error: Main method not found in class Test, please define the main method as:
public static void main(String[] args)
Actually a package-private class is accepted as main class, not sure whether that is spec-conformant or not - hmmm...
-
- Site Admin
- Posts: 10401
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
The explanation has now been enhanced. Thank you for your feedback!
Only the main method needs to be public. The specification doesn't put any requirement on visibility of the class.
HTH,
Paul.
Only the main method needs to be public. The specification doesn't put any requirement on visibility of the class.
HTH,
Paul.
-
- Posts: 38
- Joined: Fri Nov 13, 2015 4:40 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1092 :
Ah, I didn't know that the class can be package private.
Who is online
Users browsing this forum: No registered users and 5 guests