aspose file tools*
The moose likes Beginning Java and the fly likes how is this Method called? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "how is this Method called?" Watch "how is this Method called?" New topic
Author

how is this Method called?

tim huntington
Greenhorn

Joined: Aug 26, 2004
Posts: 4
i'm reading Thinking in Java,and i came across this example,how is Insect() called by only using Beetle b = new Beetle(); ? the fourth line of output is from the Insect() Method but that Method was never called,only the Beetle() Method.

//: c06:Beetle.java
// The full process of initialization.
import com.bruceeckel.simpletest.*;

class Insect {
protected static Test monitor = new Test();
private int i = 9;
protected int j;
Insect() {
System.out.println("i = " + i + ", j = " + j);
j = 39;
}
private static int x1 =
print("static Insect.x1 initialized");
static int print(String s) {
System.out.println(s);
return 47;
}
}

public class Beetle extends Insect {
private int k = print("Beetle.k initialized");
public Beetle() {
System.out.println("k = " + k);
System.out.println("j = " + j);
}
private static int x2 =
print("static Beetle.x2 initialized");
public static void main(String[] args) {
System.out.println("Beetle constructor");
Beetle b = new Beetle();
monitor.expect(new String[] {
"static Insect.x1 initialized",
"static Beetle.x2 initialized",
"Beetle constructor",
"i = 9, j = 0",
"Beetle.k initialized",
"k = 47",
"j = 39"
});
}
} ///:~
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
The compiler puts at the beginning of the Beetle() constructor. As soon as Beetle() is called, it calls Insect(). Insect() immedately calls Object().

After Object() completes, the Insect initializers are executed and rest of Insect() completes and then the Beetle initializers are executed and the rest of Beetle() completes.

Rereading my own post, I notice that the first messages come from static initializers. These are executed when the class objects are loaded. This process occurs for Insect, then Beetle, before any constructors are called.

[ August 27, 2004: Message edited by: Mike Gershman ]
[ August 27, 2004: Message edited by: Mike Gershman ]

Mike Gershman
SCJP 1.4, SCWCD in process
tim huntington
Greenhorn

Joined: Aug 26, 2004
Posts: 4
so when you extend to another class,that classes constructor is called everytime you call a class that extends to it then?
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
Yes.

The way I think of it, to build a Beetle Object, I first build a complete Object object, then turn it into a complete Insect object, and finally turn that into a complete Beetle object. The Beetle object is still an Insect object, it just has the additional state and behavior (variables and methods) of a Beetle

The first statement in a constructor is always a call to another constructor. Eventually, we get to the Object() constructor, which executes and returns to the next lower constructor. This way, calling the lowest constructor causes the constructors to execute in order from highest to lowest, without the need for some master table with all possible class relationships. You can actually change the relationships between higher level classes without recompiling either the lowest class or the methods using it and things will still work.
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
When posting code, please be sure to surround the code with the [code] and [/code] UBB Tags. This will help to preserve the formatting of the code, thus making it easier to read and understand.


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
jim gotti
Ranch Hand

Joined: Jul 02, 2002
Posts: 36
Mike,
If Java implicitly puts super() call to a 'parent' constructor, why would one even need to put super() in a constructor themselves? Or you never really have to?

hrm, maybe if the inherentence extends down say, 3 levels....and you only want the bottom level class to make a call to the constructor directly above it and not all the way up you would put super()? is that right? so if i put super() there the call will only go up one level?
[ August 28, 2004: Message edited by: jim gotti ]
Mike Gershman
Ranch Hand

Joined: Mar 13, 2004
Posts: 1272
If Java implicitly puts super() call to a 'parent' constructor, why would one even need to put super() in a constructor themselves? Or you never really have to?

You must use an explicit call to the parent constructor if you want to use a parent constructor that has parameters.

if the inherentence extends down say, 3 levels....and you only want the bottom level class to make a call to the constructor directly above it and not all the way up you would put super()? is that right? so if i put super() there the call will only go up one level?

You get no choice in the matter. At least one constructor will be called at each level until Object() is reached. Every constructor must begin with a call to super or this. If you don't put it there, Java will add super() for you. So when you call the parent constructor, that constructor must call its parent or its peer, and so on up the tree.

Keep in mind that, despite its name, a constructor just modifies an object that has already been built and initialized.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how is this Method called?
 
Similar Threads
Inheritance
java
Initialization and Inheritance
Constructors
How to add a merge array from 2 others arrays and print it