0

I just wanted to clear my concept here, so i am asking...

If I define an explicit parameterized constructor for my class, can I still invoke the default constructor provided by the java compiler (which is provided for every class by default) ??

Or does it cause result in a compile time error in such case?? Please explain what exactly happens with respect to the calls made by the compiler !!

Nike15
  • 544
  • 3
  • 16
Chitransh Saurabh
  • 377
  • 6
  • 12
  • 23

7 Answers7

11

If and only if no constructor is provided, then a no-argument constructor is created by the compiler.

The JLS states in Chapter 8:

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

The one "catch" is:

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor (§6.6) that takes no arguments and has no throws clause.

Jeremy
  • 22,188
  • 4
  • 68
  • 81
2

if you just define the parameterized constructor then compiler will not provide default constructor

and it will print an error

maxjackie
  • 22,386
  • 6
  • 29
  • 37
  • The compiler will not 'throw' an error. It will *print* an error. Exceptions are thrown, and not by the compiler. – user207421 May 31 '12 at 05:44
2

8.8.9 Default Constructor

If a class contains no constructor declarations, then a default constructor that takes no parameters is automatically provided:

  • If the class being declared is the primordial class Object, then the default constructor has an empty body.
  • Otherwise, the default constructor takes no parameters and simply invokes the superclass constructor with no arguments

.

Habib
  • 219,104
  • 29
  • 407
  • 436
2

Java Language Specification can be surprisingly helpful and actually easy to read.

MK.
  • 33,605
  • 18
  • 74
  • 111
2
  • For each an every class which haven't any constructor java will automatically added default constructor - which haven't no arguments. And no args super call.
  • If you add at lease one constructor to the class, then java will not include the default constructor.
  • As a result of that it will case a compilation error when you try to use no args constructor.

In addition to that this can lead some extra compilation errors. Test this code.

class A{
  A(int i){}
}

class B extends A{}

class Test{
  PSVM{
    B b = new B();
  }
}

This code gives a compilation error.

Class B haven't any constructor. So java will put something like this as default constructor.

B(){
  super();
}

But Supper class of class A (class B) haven't constructor that takes no args.So it guves an compilation error.

So it is better to have a no argument constructor in each class you are writing.

Keshan De Silva
  • 839
  • 1
  • 11
  • 25
  • "So it is better to have a no argument constructor in each class." No it isn't. You should provide exactly and only the constructors you need. – user207421 May 31 '12 at 05:45
1

Java provides a default no-argument constructor only for those classes which don't have a constructor explicitly defined for them. Once a constructor is defined by the programmer (even if it is a no-argument constructor), the default constructor is not provided.

Now you were asking about error:

  • If you have defined a no-argument constructor for your class, then a no-argument invocation of a constructor of your class will invoke this constructor and no compiler error will be thrown
  • if you have only defined constructor(s) which accept arguments then the no-argument invocation of your class' constructor will raise an compiler error coz no no-argument constructor will be present.

Would suggest a thorough reading of Java Language Specification

Surender Thakran
  • 3,958
  • 11
  • 47
  • 81
1

If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.

If the class being declared is the primordial class Object, then the default constructor has an empty body. Otherwise, the default constructor simply invokes the superclass constructor with no arguments.

It is a compile-time error if a default constructor is implicitly declared but the superclass does not have an accessible constructor that takes no arguments and has no throws clause.

In a class type, if the class is declared public, then the default constructor is implicitly given the access modifier public; if the class is declared protected, then the default constructor is implicitly given the access modifier protected; if the class is declared private, then the default constructor is implicitly given the access modifier private; otherwise, the default constructor has the default access implied by no access modifier.

Pramod Kumar
  • 7,914
  • 5
  • 28
  • 37