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

Static initialisation

 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can I initialise a static variable inside a non-static block,i.e {} without preceding static keyword?
suppose static varibles are initialised inside blocks {}, when these blocks are executed?
 
Author and all-around good cowpoke
Posts: 13078
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

1. you can initialize a static variable in the same statement
you declare it with.
2. static blocks are executed when the class is loaded, put a System.out.println() in a static block to see.
Bill

------------------
author of:
 
Ranch Hand
Posts: 375
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static variables are initialized when the class is first loaded & if not assigned a value they are initialized to some default value depending upon their data type .
So you could assign it a value some place else in your code but that wouldn't be initializing the variable .
Correct me if i am wrong
To understand how my code flows at run-time i use a series of println's to see it's execution , there could be a better way though . Any suggestions ?
 
Enthuware Software Support
Posts: 4810
52
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
>Can I initialise a static variable inside a non-static block,i.e {} without preceding static keyword?
Yes, you can. Because static members are accessible in non-static context too (reverse is NOT true). But it's not a good practice to do as
1.Intanse initializer will not be executed till you create an object. And so if somebody tries to access your static variable before any object of that class is created, unitialized value of the static variable will be used.
2.Instance initialializer will be executed whenever you create an object. It would be inefficient to assign the same value again and again to the same static variable!
3. It might create confusion later on.
-Paul.
------------------
SCJP2 Resources, Free Question A Day, Mock Exam Results and More!
www.jdiscuss.com
Get Certified, Guaranteed!
www.enthuware.com/jqplus

Your guide to SCJD exam!
www.enthuware.com/jdevplus
 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
non-static block{}....?
If i understand correctly you can define a static variable
inside a class.. and the default value will be used to get
it initialized based on the data type
However if its an instance method, you cant define a
static variable inside, nor access it through "this.variable"
Of, course there is always an exception.
Static main method can let you access the class variable (static)
with a reference.
Hope it helps
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Priya,
Yes, you can. Although as Paul said, it's not considered good practice to do so.
The best way to find an answer to such questions is by trying some code.

The above will compile, run and display '10'.
Hope that helps.
------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic