Page 1 of 1

About Question enthuware.ocpjp.v7.2.1492 :

Posted: Sat May 04, 2013 11:33 am
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

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

Posted: Sat May 04, 2013 12:20 pm
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.

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

Posted: Tue May 21, 2013 3:05 pm
by RobynBackhouse
So does that make it a top level class then? How come that can't be static?

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

Posted: Tue May 21, 2013 3:38 pm
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.

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

Posted: Sat May 09, 2015 10:20 pm
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();
			
	}
	
	
}

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

Posted: Sat May 09, 2015 10:49 pm
by admin
You are declaring the variable x as static, not the class. JLS explicitly says that anonymous classes can't the static.

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

Posted: Sun Jun 28, 2015 10:16 am
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

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

Posted: Sun Jun 28, 2015 10:46 am
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.

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

Posted: Sun Mar 20, 2016 6:07 am
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?

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

Posted: Sun Mar 20, 2016 9:17 pm
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.