Reputation:
I am looking through the net and finding examples of code and trying to understand them. in this one I believe super is doing nothing is that correct. in eclipe it tells me it comes from Objects class. in Java there is a tutorial.
I am trying to learn the connections between Me creating Objects into a "ArrayAdapter" into a "ListAdapter" and in the Android 4 opening template they use "ListFragment" so i would be made to belive even though i don't understand this it is corrent to learn this way of adding lists to a page for "Master Detail" selection.
links Java tutorial :: Android doc :: ArrayAdapter tutorial
I guess I just missed the boat.
public class Weather {
public int icon;
public String title;
public Weather(){
super();
}
public Weather(int icon, String title) {
super();
this.icon = icon;
this.title = title;
}
}
Upvotes: 0
Views: 77
Reputation: 136132
It is not correct to say that super() is doing nothing. It calls its super class's no-arg constructor. Even if the class inherits directly from java.lang.Object then Object's constructor is called, though it does nothing. Of course JVM optimizer will quickly eliminate such useless calls.
Upvotes: 0
Reputation: 692181
Invoking super()
in a constructor is never necessary, because if you don't, the compiler inserts this instruction for you.
So
public Foo() {
// some instructions
}
and
public Foo() {
super();
// some instructions
}
are strictly equivalent. The first thing a constructor must do is to invoke one of the superclass constructors. If you forget to do it, the compiler does it for you by inserting a super()
call. And if the superclass doesn't have a visible no-arg constructor, you'll get a compilation error.
Invoking super(someArguments)
is often necessary when you want (or need) to invoke a constructor of the base class that is not the no-arg constructor.
Upvotes: 5