Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

initilizer expressions

 
payal bansal
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi all!
class A{
int a = 10;
int b = a*c;
int c = 10;
}----->it gives compile time error, as we cant make forward referencing. But i have some confusion......
When we create an object, then first its instance variables are initialized to their default values, then super class constructor is invoked and then instance initializers or expressions are evaluated and then its own consructor gets invoked. if this is the case, then a,b,c are initially initialized to 0,0,0. Then at the time of execution of initializer expression, why do we get compile error? Do reply me at the earliest. Thanks in advance.
 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pls read JLS 12.5 Creation of new class instance:
Quote:
Whenever a new class instance is created, memory space is allocated for it with room for all the instance variables declared in the class type and all the instance variables declared in each superclass of the class type, including all the instance variables that may be hidden (�8.3). If there is not sufficient space available to allocate memory for the object, then creation of the class instance completes abruptly with an OutOfMemoryError. Otherwise, all the instance variables in the new object, including those declared in superclasses, are initialized to their default values (�4.5.5).
...
1. Assign the arguments for the constructor to newly created parameter variables for this constructor invocation.
If this constructor begins with an explicit constructor invocation of another constructor in the same class (using this), then evaluate the arguments and process that constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason; otherwise, continue with step 5.
2. This constructor does not begin with an explicit constructor invocation of another constructor in the same class (using this). If this constructor is for a class other than Object, then this constructor will begin with an explicit or implicit invocation of a superclass constructor (using super).
3. Evaluate the arguments and process that superclass constructor invocation recursively using these same five steps. If that constructor invocation completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, continue with step 4.
4. Execute the instance initializers and instance variable initializers for this class, assigning the values of instance variable initializers to the corresponding instance variables, in the left-to-right order in which they appear textually in the source code for the class. If execution of any of these initializers results in an exception, then no further initializers are processed and this procedure completes abruptly with that same exception. Otherwise, continue with step 5. (In some early implementations, the compiler incorrectly omitted the code to initialize a field if the field initializer expression was a constant expression whose value was equal to the default initialization value for its type.)
5. Execute the rest of the body of this constructor. If that execution completes abruptly, then this procedure completes abruptly for the same reason. Otherwise, this procedure completes normally.
Hope this helps.
------------------
green horn
 
Scott Appleton
Ranch Hand
Posts: 195
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Payal,
a, b, & c are all instance variables, so are initialized in the order in which they are written. At the time b is initialized, a is already initialized but c is not. Since you can't directly reference an uninitialized variable, the assignment statement int b = a*c generates a compiler error. If you place this line below the line that initializes c, you won't get the error.
 
Jason Li
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And here JLS 8.2.3.2:Restrictions on the use of Fields during Initialization

Quote:
The declaration of a member needs to appear before it is used only if the member is an instance (respectively static) field of a class or interface C and all of the following conditions hold:
1. The usage occurs in an instance (respectively static) variable initializer of C or in an instance (respectively static) initializer of C.
2. The usage is not on the left hand side of an assignment.
3. C is the innermost class or interface enclosing the usage.


------------------
green horn
 
Cameron Park
Ranch Hand
Posts: 371
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What about
class A{
int a;
int b=getC();
int c=10;
int getC(){
return c;
}
public static void main(String[] args){
A a = new A();
System.out.println(a.b);
}
}
Is the above code forwar referencing?
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Cameron,
Forward referencing doesn't apply to method calls. In your example, the method <code>int b = getC()</code> will simply use the default value of 'c' (remember fields are initialized to their default values before their initializers are run)
See JLS §8.3.2.3
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic