This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of DevSecOps Adventures: A Game-Changing Approach with Chocolate, LEGO, and Coaching Games and have Dana Pylayeva on-line!
See this thread for details.
  • 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
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

Doubt on static

 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 172
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Swapna James
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Anand!!!
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


<hr></blockquote>

pay attention to the traps from the code
[ August 22, 2005: Message edited by: Balazs Borbely ]
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 252
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi thomas ...

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

thanks & regards

srikanth reddy
 
naraharirao mocherla
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 don't always make ads but when I do they're tiny
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