Why not Constructor in Anonymous class in java?Its

2019-04-12 07:46发布

问题:

The oops rule is "No class can exist without constructor".its ok.But in java Anonymous class can'never have its constructor.because it does not have any name. So it is contradict OOPS rule..I m really confused.Is it breaking OOPS rule?Please help

回答1:

Actually, they have one implicit constructor. Suppose you have:

class A {
    A (B b, C b) {
        //constructor code
    }
}

so when you create an anonymous subclass of A via new A(b,c) {...}, it has one implicit constructor with body super(b,c). The reason that anonymous classes can't have their own explicit coustructors, I guess, is java naming convention that constructor names must match the class name. Provided that anonymous class has no name, thus you can't specify constructor for it.



回答2:

Every Java class has a constructor. You can't specify it, but your Anonymous class gets a default constructor. Nothing forces you to use an Anonymous class, you could use an inner class instead.



回答3:

In the anonymous class

Foo foo = new Foo(x) {};

the (x) specifies the actual parameters passed to the super-class constructor.

The whole anonymous class syntax is syntactic-sugar, an abbreviated syntax that the compiler translates into more basic syntactic structures.

So anonymous classes are not really anonymous. The example class above is assigned an auto-generated name like Foo$1 and it has an implied constructor of the form

Foo$1(T x) { super(x); }

where T is taken from the most-specific super-class constructor whose signature can accept the arguments (x) based on Java's normal rules for choosing among overridden signatures based on static types.



回答4:

From the Java Doc :

Anonymous class

an anonymous class is an expression. The syntax of an anonymous class expression is like the invocation of a constructor, except that there is a class definition contained in a block of code.

and

Parentheses that contain the arguments to a constructor, just like a normal class instance creation expression. Note: When you implement an interface, there is no constructor, so you use an empty pair of parentheses

EXAMPLE

class Emp{  
  public static void main(String args[]){  
    Person p=new Person(){  
       void eat(){System.out.println("nice fruits");}  
    };  

    p.eat();  
  }  
}  

Internal Implementation :

static class Emp$1 extends Person  
{  
   Emp$1(){}  

   void eat()  
    {  
        System.out.println("nice fruits");  
    }  
}

Reference : Example of Anonymous Class



回答5:

From Java Language Specification

15.9.5. Anonymous Class Declarations

An anonymous class declaration is automatically derived from a class instance creation expression by the Java compiler.

An anonymous class is never abstract (§8.1.1.1).

An anonymous class is always implicitly final (§8.1.1.2).

An anonymous class is always an inner class (§8.1.3); it is never static (§8.1.1, §8.5.1).

15.9.5.1. Anonymous Constructors

An anonymous class cannot have an explicitly declared constructor. Instead, a Java compiler must automatically provide an anonymous constructor for the anonymous class. The form of the anonymous constructor of an anonymous class C with direct superclass S is as follows:

A. If S is not an inner class, or if S is a local class that occurs in a static context, then the anonymous constructor has one formal parameter for each actual argument to the class instance creation expression in which C is declared.

The actual arguments to the class instance creation expression are used to determine a constructor cs of S, using the same rules as for method invocations (§15.12).

The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of cs.

The body of the constructor consists of an explicit constructor invocation (§8.8.7.1) of the form super(...), where the actual arguments are the formal parameters of the constructor, in the order they were declared.

B. Otherwise, the first formal parameter of the constructor of C represents the value of the immediately enclosing instance of i with respect to S. The type of this parameter is the class type that immediately encloses the declaration of S.

The constructor has an additional formal parameter for each actual argument to the class instance creation expression that declared the anonymous class. The n'th formal parameter e corresponds to the n-1'th actual argument.

The actual arguments to the class instance creation expression are used to determine a constructor cs of S, using the same rules as for method invocations (§15.12).

The type of each formal parameter of the anonymous constructor must be identical to the corresponding formal parameter of cs.

The body of the constructor consists of an explicit constructor invocation (§8.8.7.1) of the form o.super(...), where o is the first formal parameter of the constructor, and the actual arguments are the subsequent formal parameters of the constructor, in the order they were declared.

In all cases, the throws clause of an anonymous constructor must list all the checked exceptions thrown by the explicit superclass constructor invocation statement contained within the anonymous constructor, and all checked exceptions thrown by any instance initializers or instance variable initializers of the anonymous class.

Note that it is possible for the signature of the anonymous constructor to refer to an inaccessible type (for example, if such a type occurred in the signature of the superclass constructor cs). This does not, in itself, cause any errors at either compile-time or run-time.



回答6:

Even Anonymous classes have constructors, its just that you cannot give it, rather, the compiler automatically provides it for you.