File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes initilizer expressions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "initilizer expressions" Watch "initilizer expressions" New topic
Author

initilizer expressions

payal bansal
Greenhorn

Joined: May 01, 2001
Posts: 23
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

Joined: Jun 24, 2001
Posts: 46
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


green horn
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
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

Joined: Jun 24, 2001
Posts: 46
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

Joined: Apr 06, 2001
Posts: 371
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: initilizer expressions