Page 1 of 1
About Question com.enthuware.ets.scjp.v6.2.456:
Posted: Sat Dec 14, 2013 7:24 am
by devlam
What is exactly a top-level class. I've searched on the internet and from that top-level classes are classes which are not innerclasses.
But someone also stating that static class members (so static String string = new String()) are also top-level classes.
I didn't find definitions where I could judge that as right or wrong.
Re: About Question com.enthuware.ets.scjp.v6.2.456 :
Posted: Sat Dec 14, 2013 10:05 am
by admin
You may check this out:
http://docs.oracle.com/javase/specs/jls ... jls-8.html
It explains all the terms.
HTH,
Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.456 :
Posted: Wed Aug 13, 2014 10:20 am
by zidian
Can you explain what a "package member class" means? I checked the link you provided and nothing is described in that manner.
Re: About Question com.enthuware.ets.scjp.v6.2.456 :
Posted: Wed Aug 13, 2014 10:53 am
by admin
Re: About Question com.enthuware.ets.scjp.v6.2.456 :
Posted: Mon Apr 17, 2017 3:18 am
by TwistedLizard
The 2nd & 3rd sentences on that page state:
A top level class is a class that is not a nested class.
A nested class is any class whose declaration occurs within the body of another class or interface.
In this code therefore, as its declaration occurs within the body of Test, Inner1 is a nested class, not a top level class.
Code: Select all
class Test{
static class Inner1{
static class S1{ //ok
static class s2{} //ok
}
}
class Inner2{
// static class S3{} //compile error: static modifier not allowed here
}
}
and yet, it's legal to declare within it, the static class S1.
This contradicts the explanation given for this question, which says:
Only classes declared as members of top-level classes can be declared static.
As the positioning of S2 demonstrates, as long as only static classes are involved, it seems that nesting can continue to any depth.
Or, maybe I'm getting the terminology muddled up here?
Re: About Question com.enthuware.ets.scjp.v6.2.456 :
Posted: Mon Apr 17, 2017 9:21 am
by admin
The problem is that Sun (when it had control over Java) has changed its terminology a couple of times on this. Earlier, when the explanation was written, static nested class was considered a top level class. Later on they changed the terminology.
Also, in section 8.1.3, it also says, "An inner class is a nested class that is not explicitly or implicitly declared static." Thus, Inner1 is not a nested class.
Since there hasn't been any new development on the Java 6 version of the exam, it is not clear which terminology one should go with. It is your call.
HTH,
Paul.
Re: About Question com.enthuware.ets.scjp.v6.2.456 :
Posted: Mon Apr 17, 2017 2:35 pm
by TwistedLizard
That explains it.
Thank you.