About Question enthuware.ocpjp.v8.2.1444 :

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
bvrulez
Posts: 33
Joined: Sat Feb 15, 2020 12:44 am
Contact:

About Question enthuware.ocpjp.v8.2.1444 :

Post 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.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

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

Post 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?

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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);
    }
  }

}

If you like our products and services, please help us by posting your review here.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post 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.
If you like our products and services, please help us by posting your review here.

Tester
Posts: 34
Joined: Mon Oct 30, 2023 11:55 am
Contact:

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

Post 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.

admin
Site Admin
Posts: 10053
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

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

Post by admin »

We are talking about static inner class, so obviously, it has access to its enclosing class.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 38 guests