views:

1523

answers:

5

Lets say I have a concrete class Class1 and I am creating an anonymous class out of it.

Object a = new Class1(){
        void someNewMethod(){
        }
      };

Now is there any way I could overload the constructor of this anonymous class. Like shown below

Object a = new Class1(){
        void someNewMethod(){
        }
        public XXXXXXXX(int a){
          super();
          System.out.println(a);
        }
      };

With something at xxxxxxxx to name the constructor?

+9  A: 

From the Java Language Specification, section 15.9.5.1:

An anonymous class cannot have an explicitly declared constructor.

Sorry :(

EDIT: As an alternative, you can create some final local variables, and include an instance initializer in the anonymous class. For example:

public class Test
{
    public static void main(String[] args) throws Exception
    {
        final int fakeConstructorArg = 10;

        Object a = new Object()
        {
            {
                System.out.println("arg = " + fakeConstructorArg);
            }
        };
    }
}

It's grotty, but it might just help you. Alternatively, use a proper nested class :)

Jon Skeet
Thank you for copying my solution
Arne Burmeister
I actually hadn't seen your solution before I added it.
Jon Skeet
Arne, i believe him he didnt copy it. he knows enough of java to be fair enough to give credit when he would have copied it i think.
Johannes Schaub - litb
Thank you. It really helped.
sarav
+8  A: 

That is not possible, but you can add an anonymous initializer like this:

final int anInt = ...;
Object a = new Class1()
{
  {
    System.out.println(anInt);
  }

  void someNewMethod() {
  }
};

Don't forget final on declarations of local variables or parameters used by the anonymous class, as i did it for anInt.

Arne Burmeister
+1  A: 

It doesn't make any sense to have a named overloaded constructor in an anonymous class, as there would be no way to call it, anyway.

Depending on what you are actually trying to do, just accessing a final local variable declared outside the class, or using an instance initializer as shown by Arne, might be the best solution.

Ilja Preuß
The language could easily turn the "normal" constructor arguments into arguments for the anonymous class, if desired. The syntax for the constructor declaration would probably look pretty weird though...
Jon Skeet
couldn't it just say to declare the constructor like if it were the base class constructor? i don't see problems with that
Johannes Schaub - litb
A: 

Yes , It is right that you can not define construct in an Anonymous class but it doesn't mean that anonymous class don't have constructor. Confuse... Actually you can not define construct in an Anonymous class but compiler generates an constructor for it with the same signature as its parent constructor called. If the parent has more than one constructor, the anonymous will have one and only one constructor

+1  A: 

Here's another way around the problem:

public class Test{

    public static final main(String...args){

        Thread t = new Thread(){

            private String message = null;

            Thread initialise(String message){

                this.message = message;
                return this;
            }

            public void run(){
                System.out.println(message);
            }
        }.initialise(args[0]).start();
    }
}
Joel