Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to refer to the outer class in another instance of a non-static inner class?

I'm using the Apache Commons EqualsBuilder to build the equals method for a non-static Java inner class. For example:

import org.apache.commons.lang.builder.EqualsBuilder;

public class Foo {
    public class Bar {
        private Bar() {}

        public Foo getMyFoo() {
            return Foo.this
        }

        private int myInt = 0;

        public boolean equals(Object o) {
            if (o == null || o.getClass() != getClass) return false;

            Bar other = (Bar) o;
            return new EqualsBuilder()
                .append(getMyFoo(), other.getMyFoo())
                .append(myInt, other.myInt)
                .isEquals();
        }
    }

    public Bar createBar(...) {
        //sensible implementation
    }

    public Bar createOtherBar(...) {
        //another implementation
    }

    public boolean equals(Object o) {
        //sensible equals implementation
    }
}

Is there syntax by which I can refer to other's Foo reference apart from declaring the getMyFoo() method? Something like other.Foo.this (which doesn't work)?

like image 858
Kris Nuttycombe Avatar asked Nov 21 '08 18:11

Kris Nuttycombe


People also ask

How do you create an inner class instance from outside the outer class instance code?

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax: OuterClass outerObject = new OuterClass(); OuterClass. InnerClass innerObject = outerObject.

How do you access the outer class variable in an inner static class?

Unlike inner class, a static nested class cannot access the member variables of the outer class. It is because the static nested class doesn't require you to create an instance of the outer class.

When we create object of outer class the object of inner class is automatically created?

No inner class objects are automatically instantiated with an outer class object. If the inner class is static, then the static inner class can be instantiated without an outer class instance. Otherwise, the inner class object must be associated with an instance of the outer class.

What modifiers may be used with an inner class that is a member of an outer class?

Modifiers. You can use the same modifiers for inner classes that you use for other members of the outer class. For example, you can use the access specifiers private , public , and protected to restrict access to inner classes, just as you use them to restrict access do to other class members.


2 Answers

No.

The best way is probably what you suggested: add a getFoo() method to your inner class.

like image 74
Dave DiFranco Avatar answered Oct 18 '22 11:10

Dave DiFranco


No, not possible without a getter. The 'this' keyword will always point to the current instance. I'm quite curious why you would want to do this... seems like you are doing composition in the wrong way.

public class Foo {

  public Bar createBar(){
    Bar bar = new Bar(this)
    return bar;
  }
}

public class Bar {
  Foo foo;
  public Bar(Foo foo){
    this.foo = foo;
  }

  public boolean equals(Object other) {
    return foo.equals(other.foo);
  }
}

Since using Foo.this limits creation of the inner class (Foo myFoo = new Foo(); myFoo.new Bar(); to an instance I'd say this is much cleaner.

like image 21
p3t0r Avatar answered Oct 18 '22 12:10

p3t0r