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

Initialization with Inheritance

Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
I'm trying to work out in which order elements of a class are loaded.

1. Static field variables
2. Regular field variables
3. Constructors

For example: If I have the following classes.



If I create an intance of Son, in which order are the lines of code executed?

I think the first thing that happens is:
Static variables loaded starting at the top of the hierarchy [b][e][h].

But I am not 100% sure whether it then goes:

Field variable followed immediately by constructor for each subclass starting at the top of the hieracrhy: [a][c][d][f][g][i].
or
Field variables starting at the top of the hierarchy, followed by the constructors starting at the top of the hierarchy
[a][d][g][c][f][i].

Could someone put me straight on this?

Thoughts appreciated.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

See if this thread helps...

http://www.coderanch.com/t/251489/java-programmer-SCJP/certification/static-method-initialization


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Joel McNary
Bartender

Joined: Aug 20, 2001
Posts: 1821

...and just for the record (even though these are just examples), this not a very good conceptualization of inheritance. A son is not his own grandpa (country music aside), so you would not actually create this structure. (A son has a father...)

I prefer to think of inheritance like this:


...for what it's worth.
}


Piscis Babelis est parvus, flavus, et hiridicus, et est probabiliter insolitissima raritas in toto mundo.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
I think the first thing that happens is:
Static variables loaded starting at the top of the hierarchy [b][e][h].


Yes.

But I am not 100% sure whether it then goes:

Field variable followed immediately by constructor for each subclass starting at the top of the hieracrhy: [a][c][d][f][g][i].
or
Field variables starting at the top of the hierarchy, followed by the constructors starting at the top of the hierarchy
[a][d][g][c][f][i].


It will be the former as far as I know. First the constructor Son will be invoked, but the first line is implicitly super() and so Dad will be invoked, but the first line is implicitly super9) and so Grandad will be invoked. Grandad's members will be initialized, then it's constructor will finish execution and it will return to the Dad constructor. Dad's members will be initialized and then it's constructor will finish execution and return to Son's constructor. Then Son's constructor will be initialized and Son's members will be initialized and then the Son constuctor will finish execution and return to where it was originally called from.

Perhaps an easier example to work with is:



The execution order would be static fields [b][g][l] followed by the constructor's being called up the type heirarchy [m][n][h][i][c][d], followed by the initialization of members and subsequent execution of constructors for each, [a][e][f][j][k][o]. The difference is in your original example the super() line is implicit rather than explicit. Also, Object's initialization and execution of it's constructor will happen between d (where super() is called and goes to Object) and [a] (where it returns to Grandad and initializes members).

Note that this is merely the order of execution of the JVM I work with. I don't know for certain that this is guaranteed by the JLS, but it's my understanding that this is how it is. Also note that I may be talking out of my rear end as I don't often delve into details like this and have only scratched the survace and could be totally wrong. Take it as nothing more than MY understanding.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
...and just for the record (even though these are just examples), this not a very good conceptualization of inheritance. A son is not his own grandpa (country music aside), so you would not actually create this structure. (A son has a father...)


No kidding. Actually, you could make an argument for this type of inheritance but it would be exactly the opposite of how it's done here. A Son is not necessarily a dad or a grandad. But every grandad is a dad, and every dad is a son. So Grandad extends Dad extends Son wouldn't be that far-fetched.

Although I would never do something like that. There are very few times where I consider concrete inheritance a "good" solution and this would certainly not be one of them.
Arthur Blair
Ranch Hand

Joined: Sep 20, 2005
Posts: 71
Thanks for the replies.

So, keeping things simple:

Java runs the initialization and construction of classes in an inheritance hierarchy in the following order:

  • As soon as a class is accessed, all static members, starting at the top of the inheritance hierarchy are initialised. Then the static members in each subsequent child are initialised. (At this point all the field variables are also set to zero-like value)
  • Then the field variables in the class at the top of the hierarchy is initialized, immediately followed by any constructor code in the same class. This is then repeated for each child until the initial class is reached.


  • Or in a code example. The following code gets executed in the following order:
  • Statics: [b][e][h]
  • Field Variables, then Constructors: [a][c][d][f][g][i]



  • ...by the way, it's easier to see this by running a debug session in Netscape. But I didn't think of that before posting.

    Thanks again guys.

    [ November 11, 2005: Message edited by: Arthur Blair ]
    [ November 11, 2005: Message edited by: Arthur Blair ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Initialization with Inheritance