• 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 initializer vs static block

 
Greenhorn
Posts: 8
1
Netbeans IDE PHP Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?

 
Ranch Hand
Posts: 2412
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The variable two is a local variable in the static block. It doesn't exist outside the block.
 
Ranch Hand
Posts: 658
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Focusing on statics, variable scope slipped my mind. Makes perfect sense now. Thanks guys
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 79987
399
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 11604
178
Hibernate jQuery Eclipse IDE Spring MySQL Database AngularJS Tomcat Server Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Please do not shoot the fish in this barrel. But you can shoot at this 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