• 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
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

Intializers

 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello All,

1) Can anybody tell why n the static initializer below, x++ gives compiler error while x =10 doesnot.

2) The current output is 15, I guess first the initliazer loop assigns x=10 then
static int x=15
assigns it 15.
But if I just say
static int x; Shouldn't it give me 0 as output instead of 10;


public class Test4 {

static{
x=10;
// x++; To avoid Compile err replace x by Test4.x;
}

static int x=15;

public static void main(String args[]){

System.out.println(x);


}
}
 
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
umm... the output i got was 11 after i corrected the forward referencing error. You need to move this statement above the static initializer:

static int x=15;
 
K Sujit
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
umm... the output i got was 11 after i corrected the forward referencing error. You need to move this statement above the static initializer:

static int x=15;


But x = 10 in the initializer works even if declaration of x is below the loop, only when it comes it to x++ it gives error.

Another issue is - why the compiler error goes away when x is replaced by Test4.x while x = 10 works in the same loop.

static{
x=10;
// x++; To avoid Compile err replace x by Test4.x;
}
 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
i also was wondering what wa shappening with your original post

then i found this comment here http://radio.javaranch.com/corey/2004/04/19/1082390724000.html


Same flow. Static variables and static blocks will be initialized when the class itself is loaded. IIRC they will also be executed in order from top to bottom.

HTH
marco
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all

I couldn't digest the problem Can any one please explain

Thanks
 
M Mistroni
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hello,
i'll try, anyone pls jump in if my explanation is incorrec

public class Test4 {

static{
x=10;
// x++; To avoid Compile err replace x by Test4.x;
}

static int x=15;

public static void main(String args[]){

System.out.println(x);
}
}

This will result in 15 being outputted. Why?

you are not instantiating any Test4 class. so compiler will read the value you have given to x at line 15.


And what if you replace the main method lik ethis ?

public static void main(String args[]){
Test4 t = new Test4();
System.out.println(t.x);
}

result will still be 15. because when instantiating an object, like i said in my prev post, Static variables and static blocks will be initialized when the class itself is loaded. IIRC they will also be executed in order from top to bottom.

if instead you write followign code,]

public class Test4 {

static int x=15;

static{
x=10;
// x++; To avoid Compile err replace x by Test4.x;
}



public static void main(String args[]){
Test4 t = new Test4();
System.out.println(t.x);


}
}

output will be 10 (the static block is 'executed' after the static variable has been defined.. remeber? top to bottom... )


Now, why uncommenting x++ will result in an error? if i m correct, it is for the same reason i have just explained. the variable x is defined after the static block

it is the same principle if you have a code like this

public class Test4 {
int i = j;
int j = 0;
..
}

if you replace the code with this:

public class Test4 {

static int x=15;

static{
x=10;
x++; //To avoid Compile err replace x by Test4.x;
}



public static void main(String args[]){
Test4 t = new Test4();
System.out.println(t.x);

}
}
code will compile an run fine.


// x++; To avoid Compile err replace x by Test4.x;
i cannot exactly explain this, unfortunately, but x in this cas will be seen as a class variable, (and i guess if you instead use x++, compiler will consider that an instance variable)...

hth, and if some one can clarify better the last issue (the one of the x++) i will appreciate it too

HTH
marco
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Please refer the following link for forward referencing...

http://radio.javaranch.com/corey/2004/05/13/1084483439000.html

In summary, you can use x on the left hand side of the expression, but not on the right hand side, like x++, before it is declared.

Hope this helps...
 
Look! It's Leonardo da Vinci! And he brought a tiny ad!
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic