Page 1 of 1

About Question enthuware.ocpjp.v8.2.1421 :

Posted: Wed Apr 27, 2016 5:14 pm
by schchen2000

Code: Select all

class Outsider{

        public static class Insider{ // Line # 1



        }

}

class TestClass{

        public static void main(String[] args){

                Outsider os = new Outsider();

                Outsider.Insider in = Outsider.new Insider(); // Line # 2

        }

}
I've added 2 comments in the above code snippet.

The last answer option available to Problem # 53 says "You cannot refer to Insider class directly. Outsider.new Insider() is wrong as well because Insider is not static;"

I wanted to test that so I made changes and came up with Line # 2 above.

In order to test the right-hand side of the statement on Line # 2, I've modified the original code in the question by adding

"Outsider."

to

"Insider in = Outsider.new Insider()"

I ran it and Insider not being static did not seem to be the problem.

I instead encountered

"error: cannot find symbol
Outsider.Insider in = Outsider.new Insider();

symbol: variable Outsider
location: class TestClass
1 error
"

with or without "static" being added on Line # 1.

The symbol the error message refers to is "Outsider" on the right-hand side.

Am I missing something here? Thanks.

Schmichael

PS

Both classes were in the same file when being run.

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

Posted: Wed Apr 27, 2016 8:43 pm
by admin
To create a new instance of a static nested class, the syntax would be new Outsider.Insider();
The statement in the explanation is referring to the Outside dot syntax which is used for accessing a static member of Outside class. But I can see that it can easily be misinterpreted to imply that Outsider.new Insider() would be valid if Insider were static.
I have updated the explanation to make it clear.

thank you for your feedback!
Paul.

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

Posted: Wed Apr 27, 2016 9:52 pm
by schchen2000
admin wrote:To create a new instance of a static nested class, the syntax would be new Outsider.Insider();
The statement in the explanation is referring to the Outside dot syntax which is used for accessing a static member of Outside class. But I can see that it can easily be misinterpreted to imply that Outsider.new Insider() would be valid if Insider were static.
I have updated the explanation to make it clear.

thank you for your feedback!
Paul.
Thanks, Paul. Appreciate it!

Schmichael