Page 1 of 1

About Question enthuware.ocpjp.v7.2.1092 :

Posted: Wed May 29, 2013 2:36 am
by riverbox
The class MyTread is not public, this programm will not compile. A am right?

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Wed May 29, 2013 2:43 pm
by admin
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.

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Mon Sep 16, 2013 11:17 am
by The_Nick
Hi,
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:
Thanks in advance.

The_Nick.

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Mon Sep 16, 2013 12:20 pm
by admin
Did you read the explanation?

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Tue Oct 08, 2013 8:11 am
by kumarkhiani
Lovely question :)

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Fri Feb 07, 2014 2:29 pm
by EpicWestern
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".

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Thu May 08, 2014 7:39 pm
by ewebxml
The following code

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();
   }
}
prints
"Hello World"
in an infinite loop.

Question: How must the code be written so that an exception is thrown?

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Thu May 08, 2014 10:42 pm
by admin
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.

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Sun Jan 17, 2016 2:03 pm
by toolforger
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:

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)
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...

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Sun Jan 17, 2016 9:54 pm
by admin
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.

Re: About Question enthuware.ocpjp.v7.2.1092 :

Posted: Mon Jan 18, 2016 2:56 am
by toolforger
Ah, I didn't know that the class can be package private.