• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

static variables vs static initializers

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static variables (intialized)executed before static initializers
here im giving three different situations could you explain why it is giving different results...










thanks in advance
Loveleen Saroya
SCJP 1.4 in progress
 
Ranch Hand
Posts: 123
Hibernate Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Loveleen,

For first code snippet output is okey.. it happens like when class is loaded in JVM.. every static thing is executed in linear fashion from top to bottom.. say for first the static variable is initialize after that the static block is executed and the value of i is changed to 10. U can try out following code

class Init{

static int i =5;

static {
System.out.println("i is " +i);
i=10;
System.out.println("i is " +i);
}


public static void main(String [] args)
{
System.out.println(i);
}
}

output
=============
i is 5
i is 10
10
Normal Termination



But for 2nd and third code it should give compile time error as

---------- javac ----------
Init.java:20: illegal forward reference
static {i=10;}
^
1 error
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi ,

i have inserted one statement in your second code.
code:

class Init{
static int i =5; //line 1
static {
i=10;// line 2
System.out.println(i);//line 3
}
public static void main(String [] args){
System.out.println(i);
}}

//runs fine and prints 10 10

see the following code,

class Init{

static {
i=10;// line 1
System.out.println(i);//line 2
}
static int i =5;
public static void main(String [] args){
System.out.println(i);
}}


if you compile this code you will get compilation error in line 2. the error is illegal forward reference. "static block excuted before static variable initialized". the order of the static block and static variables is important.
hope this helps !

my query is ,
Line 1 assigning 10 to i before declaration . why line 1 compiles fine without error?

Thanks.

SCJP 1.4 ( progress)
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"$@nj00" please refer to my post in this thread and change your displayed name.
Thanks
-Barry

(NR) - 2
 
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am a bit confused here .What is the order of execution?Static blocks first or static variable intialization.I read in past forums that static variables are intialized first.Also the value of static variable in static block is avlaible only within the block.i.e scope of variable is within the block..

Please explain!!
Cheers
Smitha
 
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Does this help?
 
Ranch Hand
Posts: 193
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I played around with this a bit too so I would have a better idea of what's going on. Here are my results:



The result after commenting out the two println statements at the top that will not compile is:

Before Assignment Of Two: 0
After Assignment Of Two: 2
In main: 2

As someone alluded to in another thread, it has to do with where the variable is used. The left side of the equals sign seems ok, but the right side of the equals sign, or in a println, or anything that requires that the variable be resolved fails to compile and gives the "illegal forward reference" error. The assignment "myStaticInt = 1;" in the code above that occurs before the variable is declared is simply ignored as far as I can tell and the order of execution does seem to be top to bottom.

Hope that helps,
Josh
[ August 25, 2005: Message edited by: Joshua Smith ]
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks Loveleen...

for ur notice and posting that question in the forum coz that was always confusing me ...now iam comfortable..

thanks & regards

srikanth reddy
 
Loveleen Saroya
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Kedar, your link was really helpful. one more thing some people are getting compile time error because they are using jdk1.3. does the fwd reference concept came after jdk1.3?

Regards
Loveleen
 
Ranch Hand
Posts: 380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Kedar Dravid:
Does this help?



Concept is understood mentioned but the code mentioned in above link does NOT compile and I am using jdk 1.4 only.

Why is it not compiling??

For referance this is the code :
 
I will open the floodgates of his own worst nightmare! All in a tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic