File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt on static 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 "Doubt on static" Watch "Doubt on static" New topic
Author

Doubt on static

Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Hi all,

I have this doubt on static variables and static blocks.

See this code.

public class Static
{
static{

int x=5;
}
static int x,y;
public static void main(String args[])
{
x--;
myMethod();
System.out.println(x+y+ ++x);
}
public static void myMethod()
{
y=x++ + ++x;
}
}

Which one will be initialized first? The static variables or the static block?

Thanks in advance,

Swapna.


SCJP 1.4
Anand B Raju
Ranch Hand

Joined: Sep 22, 2004
Posts: 172
Static blocks will be initialized before initializing static variables.

Example:

public class Static{
static{
int x=5;
}
static int x=10;
public static void main(String args[]){
System.out.println(x);
}
}

o/p:10


SCJP 1.4<br /><a href="http://www.geocities.com/sunjava4u" target="_blank" rel="nofollow">SUN JAVA 4 U</a>- links and resources for SCJP aspirants
Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Thanks Anand!!!
Balazs Borbely
Ranch Hand

Joined: Oct 11, 2004
Posts: 33


<hr></blockquote>

pay attention to the traps from the code
[ August 22, 2005: Message edited by: Balazs Borbely ]

'Make everything as simple as possible, but not simpler.' --Albert Einstein
naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Hi Swapna

Can u plz give me the output to this program. i tried it.it is giving me

the output as 5.Is it correct.

i 've gone thro the program. and have written the println stmts to see

the output for each n every line.


first
i got the output like x-- gives -1.//that's fine.

second.after the call to method ..the output is

x=1,y=0 and ++ x= 2;

Then The final println statement (x+y + ++x) is giving the output 5.

how come???
can u plz explain???
Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Hi Naraharirao ,

This is the question from a mock exam. I have commented the values of x and y.

What will happen when you try to compile and run the code?

public class Static
{
static{
int x=5;
}
static int x,y;
public static void main(String args[])
{
x--;// Here x=-1
myMethod();//Now x=1 and y=0
System.out.println(x+y+ ++x);// 1 + 0 + 2

}
public static void myMethod()
{
y=x++ + ++x;// Here y= -1 + 1
}
}

Choose one
A) Compile Time Error
B) prints :1
C) prints :2
D) prints :3
E) prints :7
F) prints :8

Answer: D

3 is the output and I got the same. Hope this will help you,

Regards,

Swapna.
Thomas Drew
Ranch Hand

Joined: Sep 15, 2004
Posts: 47
The Static varible will be created and initilized before the static block. As Balazs point out in his post, The x created in the static block is a new x which only has scope in the static block. Instead of using int x = 5 in the static block, try using this.x = 5 or Static.x = 5. The static varible has to exist before you can work with it. Therefore, the static varible is created and initilized before the static block.
srikanth reddy
Ranch Hand

Joined: Jul 28, 2005
Posts: 252
hi thomas ...

how can we use the this operator in the static block ???

thanks & regards

srikanth reddy


Thanks & Regards<br /> <br />-Srikanth
naraharirao mocherla
Ranch Hand

Joined: Aug 16, 2005
Posts: 45
Hi Swapna
Thanks for your reply..
itried it again .yeah!!! i got the output now.

I thnk previouly i've left some of the println stmts in the program as they are.tha's why i was getting the value 5.
Now i tried it again..I got it . thank you
Swapna James
Ranch Hand

Joined: Jul 26, 2005
Posts: 56
Hi Guys,

In the explanation with the code I gave before said that the static block variable will not have any effect on the output because it declares a new variable. I got it. But what if I want to access that static block variable in the main method, can I do it? If yes how do I do it?

I am so confused about this static blocks and variables .

Regards,

Swapna.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt on static