About Question enthuware.ocpjp.v7.2.1092 :

All the posts and topics that contain only an error report will be moved here after the error is corrected. This is to ensure that when users view a question in ETS Viewer, the "Discuss" button will not indicate the presence of a discussion that adds no value to the question.

Moderators: Site Manager, fjwalraven

Post Reply
riverbox
Posts: 5
Joined: Wed Mar 27, 2013 4:14 am
Contact:

About Question enthuware.ocpjp.v7.2.1092 :

Post by riverbox »

The class MyTread is not public, this programm will not compile. A am right?

admin
Site Admin
Posts: 10401
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

The_Nick
Posts: 132
Joined: Thu May 16, 2013 9:23 am
Contact:

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

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

admin
Site Admin
Posts: 10401
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

Did you read the explanation?

kumarkhiani
Posts: 9
Joined: Sun Aug 25, 2013 2:36 am
Contact:

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

Post by kumarkhiani »

Lovely question :)

EpicWestern
Posts: 17
Joined: Wed Jan 22, 2014 12:35 pm
Contact:

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

Post 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".

ewebxml
Posts: 77
Joined: Sun Jun 30, 2013 10:04 pm
Contact:

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

Post 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?

admin
Site Admin
Posts: 10401
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

toolforger
Posts: 38
Joined: Fri Nov 13, 2015 4:40 am
Contact:

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

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

admin
Site Admin
Posts: 10401
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

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

toolforger
Posts: 38
Joined: Fri Nov 13, 2015 4:40 am
Contact:

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

Post by toolforger »

Ah, I didn't know that the class can be package private.

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests