Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I instantiate an anonymous class in the constructor of outer class?

Tags:

java

I have the following code:

public class Outer {
     public Interface Anony {
         public void callback();
     }

     public Outer() {
        OtherClass.foo(new Anony() {
            @Override
            public void callback() {
                ....
            }
        });
    }
}

But my friend told me that there is some issue in it. I created an anonymous class instance in the constructor of Outer, so the anonymous class instance implicitly references the Outer class instance, i.e. Outer.this. But at this moment, Outer class instance has not been fully created yet. So the anonymous class instance references an object with incomplete states, hence the issue.

Is he right? Thanks.

like image 927
Kai Avatar asked May 17 '13 05:05

Kai


People also ask

Can anonymous class be instantiated?

Anonymous classes enable you to make your code more concise. They enable you to declare and instantiate a class at the same time. They are like local classes except that they do not have a name.

Can an anonymous class have a constructor?

3.1. Since they have no name, we can't extend them. For the same reason, anonymous classes cannot have explicitly declared constructors.

How do you make an anonymous class constructor?

You can't. A constructor is declared using the class's name. An anonymous class, by definition, does not have a name. What you can do, however, is declare an instance initializer, just like you might do in a named class.

Can we create instance of anonymous class in Java?

We can also have an anonymous inner class that implements an interface. For example, we also know that by implementing Runnable interface we can create a Thread. Here we use an anonymous Inner class that implements an interface.

Is an anonymous class is instantiated once defined?

An anonymous class is a class not given a name and is both declared and instantiated in a single statement. You should consider using an anonymous class whenever you need to create a class that will be instantiated only once.

How do you initialize an anonymous class in Java?

Object = new Example() { public void display() { System. out. println("Anonymous class overrides the method display()."); } }; Here, an object of the anonymous class is created dynamically when we need to override the display() method.


1 Answers

You friend is right, but it depends on the usage of course.

The problem is not in creating the inner class inside the constructor. The problem will occur IF the inner class accesses the outer class.

This is because any object will not be able to give normal grantees inside a constructor. All of the variables needed for the objects operations may not have been initialized, etc.

However if the inner class is put at the end of the constructor I don't see this problem occurring, but keep in mind this is a dangerous gambit because someone may modify the code and then it's wtf time with the debugger...

like image 59
Thihara Avatar answered Nov 04 '22 13:11

Thihara