0

The Java compiler generates constructors and injects super constructor calls in many circumstances.

For example,

class Foo {
    Foo() {}
}

becomes

class Foo {
    Foo() {
        super();
    }
}

I am not keen on the different circumstances and I would like to make the code explicit.

How do you disable the Java compiler from doing this if it is possible?

Andrew McKinlay
  • 431
  • 3
  • 15

2 Answers2

1

You cannot disable the Java compiler from making calls to super - this is one of the core principles how object orientation was designed in Java.

You might however be able to tell your IDE to always display these calls (or not).

However I recommend to stick with the standard - every Java developer knows about it and what might look a bit odd and unfamiliar to you now will become perfectly reasonable after a short time... :)

Jan Groth
  • 14,039
  • 5
  • 40
  • 55
  • What do you mean by having an IDE display the calls? – Andrew McKinlay Apr 05 '14 at 22:58
  • I meant that usually an IDE will display the code like your first snippet. But there might be a setting to make it look like your second snippet. I vaguely recall that I saw something related in Eclipse, but must admit that a brief google search did not produce any results ;) – Jan Groth Apr 05 '14 at 23:16
0

Default constructor. If you don't define a constructor for a class, a default parameterless constructor is automatically created by the compiler. The default constructor calls the default parent constructor (super()) and initializes all instance variables to default value (zero for numeric types, null for object references, and false for booleans).

Default constructor is created only if there are no constructors. If you define any constructor for your class, no default constructor is automatically created.

The first statement in any subclass constructor is ALWAYS super(). There is no need to make a call to it because it will be supplied automatically if the superclass have a default constructor without params.

If the parent class doesn't have a default constructor, you have to add an super(params) call.

Remember all classes implicitly will extend Object if they do not extend any class explicitly

alfcope
  • 2,327
  • 2
  • 13
  • 21
  • Will Java generate a call to an explicitly defined zero-argument constructor? Does the term "default constructor" refer to any zero-argument constructor or only to an implicitly generated zero-argument constructor? – Andrew McKinlay Apr 05 '14 at 22:16
  • Yes, it will. "Default constructor" is the zero-argument constructor, explicitly defined or implicitly generated. It does not matter. – alfcope Apr 05 '14 at 22:21
  • Not according to this [answer](http://stackoverflow.com/a/4488766/881334) and the corresponding Java Language Specification [excerpt](http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9). – Andrew McKinlay Apr 05 '14 at 22:50
  • Explicitly defined constructor with no arguments is like the default constructor. You only have to do a small and simple test to check it. – alfcope Apr 05 '14 at 23:01
  • Right. A user-defined zero-argument constructor can have the same behavior as the default constructor, but when you define it yourself, it is not called the default constructor. – Andrew McKinlay Apr 05 '14 at 23:13