Page 1 of 1

About Question enthuware.ocpjp.v8.2.1444 :

Posted: Thu Feb 20, 2020 9:19 am
by bvrulez
I was confused by the fact that the second option had an implemented method despite being abstract. But of course this is possible.

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

Posted: Sat Dec 09, 2023 6:00 pm
by Tester
Could you give an example for
You can access d from a static inner class by using a reference to an instance of Outer.
If I understand right, I need to send an Outer's reference as a parameter to a method of the static Inner class, or?

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

Posted: Sun Dec 10, 2023 6:16 am
by admin
You do not necessarily need to send a reference as an argument. You can also do this:

Code: Select all

class Outer
{
    private double d = 10.0;

   static class SI {

    {
       System.out.println(new Outer().d);
    }

    void m(){
      System.out.println(new Outer().d);
    }
  }

}


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

Posted: Sun Dec 10, 2023 4:19 pm
by Tester
Thank you a lot. It looks strange. You create new instance of Outer class. SI and method m() have different instances of Outer(). You may create any instance. There is no any connection between Outer and SI only name space. Sorry its not fully clear. Could you please give a link to clarify.
Thank you a lot one more time.

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

Posted: Sun Dec 10, 2023 8:08 pm
by admin
In the example I showed above, I am accessing Outer's d from a static inner class. Yes, they are not same instances. If you want same Outer instance, of course you have to share that instance's reference some how. I am not sure which part is not clear to you and which link are you asking for.

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

Posted: Mon Dec 11, 2023 3:05 am
by Tester
You can access d from a static inner class by using a reference to an instance of Outer.
Static class does not have access to "this" (if i do not use DI, for example) it means that it does not have access to instance. If you create new instance it means that you have access to class and may create an instance. Please correct me if I wrong. I think now its clear. Thank you.

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

Posted: Mon Dec 11, 2023 9:29 am
by admin
We are talking about static inner class, so obviously, it has access to its enclosing class.