Page 1 of 1

About Question enthuware.ocpjp.v8.2.1492 :

Posted: Mon Sep 19, 2016 3:58 am
by ramy6_1
Hello ,

Not sure what is meant by 'package member classes'.

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

Posted: Mon Sep 19, 2016 10:20 am
by admin
A class that is a member of a package as opposed to a member of a class.

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

Posted: Thu Nov 24, 2016 7:55 pm
by jamesfeng2
why below can be compiled and print hello?
That against "the modifier static pertains only to member classes, not to top level or local or anonymous classes."

package p2;

abstract class cd76 {

public static void main(String[] args) {
System.out.println("hello");

}
}

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

Posted: Thu Nov 24, 2016 10:11 pm
by admin
The main method is a static method. An instance of the class cd76 is not required to run it. Also, the main method is not creating any instance of class cd76. So there is no reason for it to not compile and run.
Why do you think it should not compile and run?
-Paul.

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

Posted: Mon Jan 30, 2017 3:35 pm
by runnerdave
one of the options is:
Anonymous classes cannot be declared static.

that is true as well, how could I know which ones to mark then?

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

Posted: Mon Jan 30, 2017 10:11 pm
by admin
runnerdave wrote:one of the options is:
Anonymous classes cannot be declared static.

that is true as well, how could I know which ones to mark then?
You have to select 2 correct options and this is one of the 2 correct options. So you should select option 2 and 4.

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

Posted: Thu Jul 26, 2018 8:23 am
by __JJ__
admin wrote:
Mon Sep 19, 2016 10:20 am
A class that is a member of a package as opposed to a member of a class.
So, are the terms "package member class" and "top-level class" synonymous?

TIA.

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

Posted: Thu Jul 26, 2018 9:40 am
by admin
Yes, as per Section 7.1 of JLS:
The members of a package are its subpackages and all the top level class types (§7.6, §8 (Classes)) and top level interface types (§9 (Interfaces)) declared in all the compilation units of the package.

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

Posted: Thu Aug 02, 2018 5:13 pm
by __JJ__
Hi Admin
I'm just going over some notes and I found this note that I made a few days ago:
Anonymous classes cannot be declared static.
which got me thinking

Code: Select all

class T29 {   
    static Runnable r = new Runnable(){ 
        public void run(){}
    };
}
In what way is this not an anonymous class, and a static one at that? My understanding is that an anonymous class is a class definition combined with instantiation.
Thank you very much.

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

Posted: Thu Aug 02, 2018 8:51 pm
by admin
The variable r is static. Not the anonymous class.

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

Posted: Thu Aug 02, 2018 11:30 pm
by __JJ__
admin wrote:
Thu Jul 26, 2018 9:40 am
Yes, as per Section 7.1 of JLS:
The members of a package are its subpackages and all the top level class types (§7.6, §8 (Classes)) and top level interface types (§9 (Interfaces)) declared in all the compilation units of the package.
OK, thank you.

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

Posted: Thu Aug 02, 2018 11:38 pm
by __JJ__
admin wrote:
Thu Aug 02, 2018 8:51 pm
The variable r is static. Not the anonymous class.
Yes of course. But...I don't see then what else there is to try to declare static. There's nothing to an anonymous class apart from

Code: Select all

Runnable r = new Runnable(){...} 
The only conceivable place (other than where I used it) that one could use the static keyword is before "new" which would be .. weird.

Code: Select all

Thread t = new Thread(static new Runnable(){...}); 
Anyway, I will remember that ACs cannot be declared static.
Thank you.

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

Posted: Thu Aug 02, 2018 11:44 pm
by admin
That is why it is not static. You can use javap to inspect the class file created for the anonymous class. Create a named static nested class and use javap on that to compare.

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

Posted: Fri Aug 03, 2018 7:04 am
by __JJ__
admin wrote:
Thu Aug 02, 2018 11:44 pm
That is why it is not static. You can use javap to inspect the class file created for the anonymous class. Create a named static nested class and use javap on that to compare.
I'll try that, thanks very much.

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

Posted: Thu Sep 12, 2019 9:40 pm
by admin
Post by sreeharshitha » Fri Sep 13, 2019 12:50 am

if package member classes and top level classes are synonymous, why isn't option 1 correct?

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

Posted: Thu Sep 12, 2019 9:44 pm
by admin
Option 1 says, "Package member classes can be declared static." But they can't be. That is why it is an incorrect option.

(Sorry, your original post got deleted by mistake. So, I recreated it.)