• Post Reply Bookmark Topic Watch Topic
  • New Topic

construction of an object  RSS feed

 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I realized that i don't really understand the steps that are made when an object is constructed. I have the following knowledge about an object being created in Java :
During the construction of an object, for example object X, the following steps will be taken :
1.The first time an object of type X is created or the first time a static method or static field of class X is accessed, the Java interpreter must locate X.class, which it does by searching through the classpath.
2.When X.class is located, it is loaded into memory.
3.When the class-file is loaded into memory a Class object is created, the static members are initialized and the static initializers are run. This Class object is used to create all objects of that type.
4.To construct the X object, the construction process allocates enough storage for a X object on the heap.
5.This storage is wiped to zero, automatically setting all the instance variables to there default value
This step, setp 5, is repeated recursively such that the root of the hierarchy is constructed first, followed by the
next-derived class, etc. until the most-derived class is reached.
At this point in the construction of object X, the storage is allocated and instance variables are set to their default value :
The following steps are repeated recursively such that the root of the hierarchy takes them all first, followed by the next-derived class, etc. until the most derived class is reached.
6.The instance variables of the object are initialized with their initialization value in order of declaration.
7.All object initializers are executed in order of declaration.
8.The body of the constructor is executed.
I don't really understand the following construction :
public class X extends Y{
private final int s = 1;
private final int t = 2;
public X() {
super(s,t);
int z = 2;
switch(z) {
case s:
case t:
}
}
}
class Y {
public Y(int s, int t) {
}
public static void main(String[] s) {
new X();
}
}
I have the following question about this construction :
Why can't i make calls to instance variables declared in this class or any superclass? Even notice that s and t are compile time constants, that's why i added the switch-statement. I would really appreciate it when you could answer my question based on my current knowledge and could add a little step that makes the construction understandable.
 
Arnold Reuser
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe i should add that the call super(s,t) results in a compile time error.
 
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a good, tricky question.
Although s and t are indeed available as compile-time constants, they're actually allocated space in the object, as well. Remember that Java has "blank finals" -- final variables which are initialized at runtime, so finals act like regular members in many ways.
The answer to your question is just that the JLS doesn't make a special exception for final values. The rule is simply that you can't use any members of a class in the process of invoking "super".
 
Arnold Reuser
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First of all. Thnx for answering my question, i appreciate it.
In a few days I'm trying to pass my SJCP exam so that's why I am asking a lot of questions. I just made up some questions for myself and I'm trying to answer them. I've read your answer several times and realized that you're just saying what I was afraid of : Just accept it.
I didn't understand the previous construction in the first place because of the following construction :
public class X {
private int x = 2;
public int getWhy() {
return x;
}
public X() {
int y = getWhy();
System.out.println(y);
}
public static void main(String[] s) {
new Y();
}
}
class Y extends X{
private int x = 1;
public int getWhy() {
return x;
}
}
As you know 0 will be printed because of the fact that :
1. method getWhy of X is overridden in Y. &
2. the initialization of Y takes place after the constructor of X completes.
That's why I didn't understand it and thought I did make an error somewhere.
But how can a language accept this type of construction on the one hand. And don't accept a construction like for example super(this.getWhy()) on the other hand. Or even a construction like I had given in the previous example. Do you know where I can find more information for this type of questions? So that I can read it before I take my SJCP exam.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, the ultimate source for this sort of info is the Java Language specification. In section 8.8.5.1, we'll find

An explicit constructor invocation statement in a constructor body may not refer to any instance variables or instance methods declared in this class or any superclass, or use this or super in any expression; otherwise, a compile-time error occurs.

and in section 12.5:

Unlike C++, the Java programming language does not specify altered rules for method dispatch during the creation of a new class instance. If methods are invoked that are overridden in subclasses in the object being initialized, then these overriding methods are used, even before the new object is
completely initialized.

So yeah, that's just the way it was defined. There are no rationales given for either of these points; often this means that the rule fell out of the original implementation.
See here for the online edition of the JLS.
 
Arnold Reuser
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'll read the JLS, thnx.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!