28

Ok, So I know that an anonymous inner class is either implicitly extending a parent class or implementing an interface, and therefore a constructor of the superclass will need to be called. However, I'm not sure how to create a constructor for the anonymous class (if this is possible) and without defining a constructor I'm not sure how to make calls to super()! Here is my practice code:

public class AnonymousConstructor {
    public static void main(String[] args) {
        //I'm not sure how to explicitly call one of the arg super constructors
        MyBob my = new MyBob() {
            //I would like to do something like this super("String"); or      
            //super("String", "String");
        };

    }
}

class MyBob extends Thread {
    MyBob() {
        System.out.println("No arg constructor");
    }
    MyBob(String a) {
        System.out.println("Arg constructor");
    }
    MyBob(String a, String b) {
        System.out.println("2 arg constructor");
    }
    public void run() {
        System.out.println("Outer");
    }
}

My concern is that if you try to make an anonymous class from a class that doesn't have a no-arg constructor that the code will fail at compile time because there is no way to pass an argument to the superconstructor. Is this a valid concern, and if so, is there a way around this?

Sled
  • 18,541
  • 27
  • 119
  • 168
paniclater
  • 903
  • 1
  • 12
  • 18

3 Answers3

38

You can't define a constructor for an anonymous class (part of the language specification), but you can control which super constructor is called by simply providing arguments to the new call:

MyBob my = new MyBob("foo") { // super(String) is called
    // you can add fields, methods, instance blocks, etc, but not constructors
}
n611x007
  • 8,952
  • 8
  • 59
  • 102
Bohemian
  • 412,405
  • 93
  • 575
  • 722
  • 1
    Thank you, this answers my question. I now understand that in my code I can't create a constructor for my inner class but I can pass arguments into the new call for example MyBob my = new MyBob("foo", "bar") { }; Thanks! – paniclater Dec 28 '13 at 22:37
  • 1
    This is, in fact, required if the superclass does not have a no-arg constructor. – Evgeni Sergeev Oct 19 '18 at 16:30
7

Every class (without a specific constructor) has a no-arg constructor by default. An empty constructor will be inserted and javac will place a super() call.

In your current example, you could say

new MyBob() {
  // anonymous MyBob sub-class 1, uses No arg constructor.
}
new MyBob("test") {
  // anonymous MyBob sub-class 2, uses Arg constructor.
}

or

new MyBob("test", "ing") {
  // anonymous MyBob sub-class 3, uses 2 arg constructor.
}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Is that so? How is it then that I can't code `Integer i = new Integer();`? – Bohemian Dec 27 '13 at 20:19
  • 1
    @Bohemian the Integer class is declared as final (like const in c++) so it cannot be constucted. The integer class has useful static methods like Integer.parseInt("1234"); – Katianie Dec 27 '13 at 20:23
  • @Katianie Wrong. Hint: You can code `Integer i = new Integer(0);`. Bigger hint: Read comment by @his – Bohemian Dec 27 '13 at 20:25
  • I understand that a class with no constructors gets a default constructor, but my question was more how to specify a constructor for the super class of an anonymous inner class when there was no default no-arg constructor due to a single or multi arg constructor being explicitly set and no no-arg constructor being implicictly set. – paniclater Dec 28 '13 at 22:35
0

In Java, you need to have a constructor no matter what. It can be as simple as

    public AnonymousConstructor()
    {
    }

Also there is no }; in java, simply dont put that semicolon. Also you always want to use access modifiers ie "public, private, or protected". Now you should be able to call the super consuctor.

For the main class, it is static and thus does not need a constuctor. If you want to make a method in the main class it must be static.

Katianie
  • 589
  • 1
  • 9
  • 38