aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Restrictions on the use of Fields during Initialization 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 "Restrictions on the use of Fields during Initialization" Watch "Restrictions on the use of Fields during Initialization" New topic
Author

Restrictions on the use of Fields during Initialization

Gurwinder Prince
Greenhorn

Joined: Jan 18, 2001
Posts: 6
Hello! Anyone can Help me in undersatanding the process.
class Test {
Test() { k = 2; }
int j = 1;
int i = j;
int k;
}class Z {
static int i = j + 2;
static int j = 4;
}
and:
class Z {
static { i = j + 2; }
static int i, j;
static { j = 4; }
}
=================================================================
class Z {
static int peek() { return j; }
static int i = peek();
static int j = 1;
}
class Test {
public static void main(String[] args) {
System.out.println(Z.i);
}
}
=================================================================

1.class UseBeforeDeclaration {
2.static {
3.x = 100; // ok - assignment
4.int y = x + 1; // error - read before declaration
5.int v = x = 3; // ok - x at left hand side of assignment
6.int z = UseBeforeDeclaration.x * 2;
7.// ok - not accessed via simple name
8.Object o = new Object(){
9.void foo(){x++;} // ok - occurs in a different class
10.{x++;} // ok - occurs in a different class
11. };
12. }
13.{
14.j = 200; // ok - assignment
15.j = j + 1; // error - right hand side reads before declaration
16.int k = j = j + 1;
17.int n = j = 300; // ok - j at left hand side of assignment
18.int h = j++; // error - read before declaration
19.int l = this.j * 3; // ok - not accessed via simple name
20.Object o = new Object(){
21.void foo(){j++;} // ok - occurs in a different class
22.{ j = j + 1;} // ok - occurs in a different class
23.};
24.}
25.int w = x= 3; // ok - x at left hand side of assignment
26.int p = x; // ok - instance initializers may access static fields
27.static int u = (new Object(){int bar(){return x;}}).bar();
28.// ok - occurs in a different class
29.static int x;
30.int m = j = 4; // ok - j at left hand side of assignment
31.int o = (new Object(){int bar(){return j;}}).bar();
32.// ok - occurs in a different class
33.int j;
34. }
According to java tutorials the above code
(class usebeforedeclaration) is right but when i compiled that code using jdk1.2.1 it is giving errors.

Anshuman Acharya
Ranch Hand

Joined: Jan 19, 2001
Posts: 144
i will treat your examples one by one:
the first code gives a compile error coz in static/non-static initialization the statements are executed in the order they are orientated. static statements and code blocks are executed at the time of the creation of the class, i.e, compile time. instance variable initialzation and execution of instance block codes is at the time of the instantiation of the class.
also, forward referencing of variables can only be done via methods.
so coming back to #1,
static int i = j + 2;//forward referenced j gives error
static int j = 4;
#2
static { i = j + 2; }//forward referencing again of i and j
static int i, j;//compile-time error
#3
compiles. forward referencing thru a method.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Restrictions on the use of Fields during Initialization
 
Similar Threads
Initialization
Forword Refferencing Question
Something weird in JLS (or in JBuilder)
strange code from Jls about initialization of fields
Field Initializations