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 puzzle 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 "puzzle" Watch "puzzle" New topic
Author

puzzle

Oscar Garcia
Greenhorn

Joined: Mar 05, 2002
Posts: 23
Can anyone explain why the ff. code produces this output?
I thought i could get 99 as an output...
class puzzle{
static private int i = getK();
static private int k = 99;

static private int getK(){
return k;
}
public static void main(String[] args){

System.out.println("i-->" + i);

}
}//end class
//output: i-->0


Oscar S. Garcia<br />SCJP<p>--------------------------<br />Pilipino ako...ayos ba?<br />--------------------------
R K Singh
Ranch Hand

Joined: Oct 15, 2001
Posts: 5371
hi
As per JLS The static initializers and class variable initializers are executed in textual order.
so when static int i is get initialized by calling getK(), static int k has NOT been initialized as it comes after var 'i', so it has a default value of '0', which get perinted.
HTH


"Thanks to Indian media who has over the period of time swiped out intellectual taste from mass Indian population." - Chetan Parekh
rajiv nair
Greenhorn

Joined: Feb 07, 2002
Posts: 15
The static initializers and class variable initializers are executed in textual order.
If you change the code as
class Puzzle
{
static private int k = 99;
static private int i = getK();

static private int getK()
{
return k;
}
public static void main(String[] args)
{
System.out.println("i-->" + i);
}
}
you will get 99 as output.
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
Hi Rajiv, you correctly pointed out the output of the above code, but I am not sure of the explanation as yet.
By "textual order" do we mean that i will be initialized before k, provided both are static? if so, why just putting k=99 above the i=getK() method initialized i to 99?
Could you elaborate a little, Thanks!


Anup Engineer
Mario Levesque
Ranch Hand

Joined: Nov 01, 2000
Posts: 51
Anup,
The variables will be initialized by order of execution one line at a time, ie. line of code on top first then line of code below after. This must be what others refer to as "textual order" it does not mean alphabetical order.
So when you assign a variable to a literal then call a method after, it works as expected, but when you call the method before the variable has been initialized to a literal you get omething else. That's because the default of zero is substituted in the variable inside the method since it has not been declared and initialized yet.
Does this help?
Ask again if not.
Cheers,


<a href="http://www.ajmasters.com" target="_blank" rel="nofollow">http://www.ajmasters.com</a> Real Estate, Tampa Florida
Anup Engineer
Ranch Hand

Joined: Mar 04, 2002
Posts: 48
That does help Mario..the "textual order" confused me..but now I understand it.
Cheers!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: puzzle
 
Similar Threads
Puzzle for JavaProgrammers
SoDuko puzzle
A Puzzle...sort of
Puzzle