This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Initializers... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Initializers..." Watch "Initializers..." New topic
Author

Initializers...

Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78


Is access by methods behave differently ..?
help needed to understand the difference between 1 and 2 example...


SCJP 1.4<br />" Something is difficult doesn't mean you shouldn't try, it only means you should try harder "
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
class Z
{
static { i = j + 2; }
static int i, j;
static { j = 4; }
}

In the above case,
static initializer expressions are executed in textual order. Code in a static initializer block is subject to the declaration-before-red rule. Forward references made on the left hand side of the assignment is always allowed. In the above case, the static initializer block: static{i=j+2;}
tries to read the value of j before it has been declared.
Hence the compile-time error.

The explanation for the second case can best be understood by going through Corey's SCJP tipline.
Anyway, here's the explanation in short:
The compiler will look for simple cases of forward references, but it won't go thru' your methods to see if you are performing an illegal reference.
The reason u get 0 as the o/p is because 0 is the default value of the int data type.
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78
Thats great..
thanks...
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
This should make things even more clear:
public class CDummy
{
static
{
xx = 9;
}

public static void main(String args[])
{
System.out.println("xx = " + xx);
}

static int xx = 7;
}

Output:
xx=7

Explanation:
The static variable is defined at the entry in the class (just like all class variables). After that, the static initializers are executed in the 'textual' order:
first: xx = 9;
second: xx = 7;
Soumy Kumar
Ranch Hand

Joined: Nov 02, 2004
Posts: 78

The static variable is defined at the entry in the class (just like all class variables). After that, the static initializers are executed in the 'textual' order:
first: xx = 9;
second: xx = 7;


According to your explanation..doesn't it mean that first xx gets the value of 7 and then the static initializers are executed which means xx value is 9.
so when main method is executed the value of xx= 9. right ???

explanation needed ..
Kedar Dravid
Ranch Hand

Joined: May 28, 2004
Posts: 333
static
{
xx = 9;
}
is the first static initialiser.

static int xx = 7;
is the second static initialiser.

So, when main() is called, xx=7;
On entry to the class, the static variable xx is only defined, not initialised.
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
I compiled the program as u told:

public class CDummy
{
static
{
xx = 9;
}

public static void main(String args[])
{
System.out.println("xx = " + xx);
}

static int xx = 7;
}


But i am getting compilation error saying cannot reference a field before its declared.
But if i give something like this
public class CDummy
{
//static
{
xx = 9;
}

public static void main(String args[])
{
System.out.println("xx = " + xx);
}

static int xx = 7;
}
I get the output as xx = 7
So this means that since statics are initialized first so the statement
static int xx = 7 gets executed first.
Well what abt instance initailzer { xx = 9; }, how do i get the value of xx as 9? when does this gets associated?
Kristof Janssens
Greenhorn

Joined: Jan 05, 2005
Posts: 23
Originally posted by Animesh Shrivastava:

public class CDummy
{
static
{
xx = 9;
}

public static void main(String args[])
{
System.out.println("xx = " + xx);
}

static int xx = 7;
}


I don't know what you're doing wrong, but this piece of code just works fine here (like I expected)

And Kedar Dravid statement is true. First the static variable is created and set to it's default value, so in this case, 0.

After that, the classloader will process the class from top to bottom. It will assign xx to 9 first, after that it will assign xx to 7.

So int xx = 7; isn't executed at the same time.

Also note that as someone already said in the this thread, you can only set a value of a variable before it is created in the source code. You can't read it:

{int i = j;} //read a variable
int j = 0;
==> it will give an error

{j = 0;} //set a variable
int j;
==> this will work

This is the sequence that the classloader follows:

1. static + instance variables declaration + default value assigment
2. static initializer block execution + static variable value assignment
3. static void main
4. initializer block execution + instance variables value assignment
5. constructor

Anyway, this is what I found after testing this. If you want, I can place here an code example to show the sequence that the classloader follows.
[ February 14, 2005: Message edited by: Kristof Janssens ]

SCJP 1.4 (90%)<br />SCJD (in progress)
Animesh Shrivastava
Ranch Hand

Joined: Jul 19, 2004
Posts: 298
Ok,May be i am using an older Java version, its 1.3, so its happening
I will look in the 1.4 version and let u know
Thanks
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Initializers...
 
Similar Threads
Forward Referencing
Question on static initializers..
Restrictions on the use of Fields during Initialization
accessing static values
Why is it so?