About Question enthuware.ocpjp.v7.2.1492 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
RobynBackhouse
Posts: 23
Joined: Sun Apr 14, 2013 10:37 am
Contact:

About Question enthuware.ocpjp.v7.2.1492 :

Post by RobynBackhouse »

Hi,
Can you please explain exactly what is a Package member class?
I assumed it was just a class within a package.. but obviously not.
Thanks

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

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

Post by admin »

Yes, it is just a class defined in a package. Not inside any other class. Package member is a standard terminology used by oracle: http://docs.oracle.com/javase/tutorial/ ... epkgs.html

HTH,
Paul.
If you like our products and services, please help us by posting your review here.

RobynBackhouse
Posts: 23
Joined: Sun Apr 14, 2013 10:37 am
Contact:

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

Post by RobynBackhouse »

So does that make it a top level class then? How come that can't be static?

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

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

Post by admin »

Because you can't define a package level class as static. It doesn't make any sense. Only a nested class (i.e. a class defined instead another class) can be defined as static.

-Paul.
If you like our products and services, please help us by posting your review here.

ThufirHawat
Posts: 28
Joined: Wed Feb 25, 2015 9:03 am
Contact:

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

Post by ThufirHawat »

I don't agree with that answer:

"Anonymous classes cannot be declared static."

You can do that:

Code: Select all

public class X extends Thread{
	
	static Runnable x = new Runnable(){

		@Override
		public void run() {
			
			System.out.println("Test");
			
		}
		
	};
	
	
	public static void main (String ...strings){
		
		Thread t = new Thread(new X().x);
		t.start();
			
	}
	
	
}

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

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

Post by admin »

You are declaring the variable x as static, not the class. JLS explicitly says that anonymous classes can't the static.
If you like our products and services, please help us by posting your review here.

Danny Sheridan
Posts: 30
Joined: Sat May 02, 2015 4:48 pm
Contact:

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

Post by Danny Sheridan »

...only classes declared as members of top-level classes can be declared static.
Should this be:
Classes declared as members of top-level non-static classes can be declared static
Also, classes declared as members of any static class can be declared static

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

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

Post by admin »

Danny Sheridan wrote:
...only classes declared as members of top-level classes can be declared static.
Should this be:
Classes declared as members of top-level non-static classes can be declared static
No, the original statement is fine because you can't have a top level static class.
Also, classes declared as members of any static class can be declared static
Yes, if you consider multiple levels of nesting, then you could say that.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.

sumanenthu
Posts: 23
Joined: Sun Feb 21, 2016 10:12 am
Contact:

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

Post by sumanenthu »

Anonymous classes cannot be declared static. - Regarding this statement, can we assume that its true because of the fact that there is no scope of declaring it but instantiating it when required. Or can we declare it by any means which will not compile?

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

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

Post by admin »

Syntax of an anonymous class includes the declaration as well as instantiation. And the syntax doesn't allow the use of static keyword, effectively preventing you from creating a static class.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 98 guests