Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static initializer vs static block

 
Teo Rajkumar
Greenhorn
Posts: 8
1
Netbeans IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm currently studying for my OCA se8 and having been creating lots of simple code examples. I have created this Statics class to play around with the concepts of statics but I encountered this issue.
I'm calling both int values using the class name. Statics.two does not compile but Statics.one does.
Is "static int var=value1" not equivalent to "static{ int var2=value2}" ? If so how do they differ?

 
Keith Lynn
Ranch Hand
Posts: 2409
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The variable two is a local variable in the static block. It doesn't exist outside the block.
 
Puspender Tanwar
Ranch Hand
Posts: 397
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
kieth i right
a variable declared in a block (whether it is a instance or static block, or a method block or if, switch etc etc block ) cannot be used outside that block.

Teo Rajkumar wrote: Is "static int var=value1" not equivalent to "static{ int var2=value2}" ? If so how do they differ?

var is a class variable which can be shared among the whole class or outside the class(depending on its access modifier) whereas var2 is a local variable having scope upto static block only.
 
Teo Rajkumar
Greenhorn
Posts: 8
1
Netbeans IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Focusing on statics, variable scope slipped my mind. Makes perfect sense now. Thanks guys
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Rajkumar,
Warm welcome here,
Do you know,here we have an special dedicated section,meant only to help or give guidance for OCA
You can share all your doubts,which you face during the preparation,and be ready to get the answer,in no time

As you mentioned you are practicing static,do read this
 
Campbell Ritchie
Sheriff
Posts: 51446
87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sachin Tripathi wrote:. . . we have an special dedicated section,meant only to help or give guidance for OCA . . .
Good suggestion: shall move thread there.

And welcome again
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Teo Rajkumar,

First of all, a warm welcome to CodeRanch!

Teo Rajkumar wrote:Is "static int var=value1" not equivalent to "static{ int var2=value2}" ? If so how do they differ?

Definitely not! In this code snippetvar is a static (or class) variable and var2 is nothing more but a local variable inside the static initializer block. And because var2 is a local variable, you can only access it in the static initializer block itself.

But you could use the static initializer block to initialize static (or class) variables (and then it's equivalent to a static initializer), as illustrated in the following code snippetOutput:
static: var1 = 1
static: var2 = 2
static: var3 = 3
main: var1 = 1
main: var2 = 2


And as a final remark, a static (or class) variable can have the same name as a local variable. Illustrated in this code snippetAnd the same applies to instance variables as wellWhat do you think the output will be of both code snippets?

Hope it helps!
Kind regards,
Roel
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic