Wednesday, March 30, 2011

Java constructors

Here are some legal and illegal examples:

class ConstructorExamples {
    // legal constructors
    ConstructorExamples () { }
    private ConstructorExamples (byte b) { }
    ConstructorExamples (int x) { }
    ConstructorExamples (int x, int... y) { }

    // illegal constructors
    void ConstructorExamples () { }           // it's a method, not a constructor
    ConstructorLike () { }                    // not a method nor a constructor
    ConstructorExamples (short s);            // looks like an abstract method
    static ConstructorExamples (float f) { }  // can't be static
    final ConstructorExamples (long x) { }    // can't be final
    abstract ConstructorExamples (char c) { } // can't be abstract
    ConstructorExamples (int... x, int t) { } // bad var-arg syntax
}

No comments:

Post a Comment