About Question enthuware.ocpjp.v7.2.1492 :
Moderator: admin
-
- Posts: 23
- Joined: Sun Apr 14, 2013 10:37 am
- Contact:
About Question enthuware.ocpjp.v7.2.1492 :
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
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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
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.
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Sun Apr 14, 2013 10:37 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
So does that make it a top level class then? How come that can't be static?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
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.
-Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 28
- Joined: Wed Feb 25, 2015 9:03 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
I don't agree with that answer:
"Anonymous classes cannot be declared static."
You can do that:
"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();
}
}
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
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.
-
- Posts: 30
- Joined: Sat May 02, 2015 4:48 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
Should this be:...only classes declared as members of top-level classes can be declared static.
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
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
Yes, if you consider multiple levels of nesting, then you could say that.Danny Sheridan wrote:Should this be:...only classes declared as members of top-level classes can be declared static.No, the original statement is fine because you can't have a top level static class.Classes declared as members of top-level non-static classes can be declared staticAlso, classes declared as members of any static class can be declared static
HTH,
Paul.
If you like our products and services, please help us by posting your review here.
-
- Posts: 23
- Joined: Sun Feb 21, 2016 10:12 am
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
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?
-
- Site Admin
- Posts: 10058
- Joined: Fri Sep 10, 2010 9:26 pm
- Contact:
Re: About Question enthuware.ocpjp.v7.2.1492 :
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.
Who is online
Users browsing this forum: No registered users and 98 guests