• 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
  • Paul Clapham
  • Ron McLeod
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Rob Spoor
  • Devaka Cooray
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Jj Roberts
  • Al Hobbs
  • Piet Souris

Static

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am a new member. I feel this forum will help us a lot in SCJP.I have a problem in this :

I don't know what is "static" in line 1. If its a function whose return type is static ,why is it getting called when I am not calling it. What I know is only default constructor gets called in line 2.

(code tags added)
[ May 15, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch Meeta.
The static block starting at line ///line1 is what is known as a "static initializer block". It is a block of code which is executed once when the class is loaded. The code inside the static initializer block is normally used to initialize the static variables of the class. The code can also produce side effects such as connecting to a database or opening files, etc.

Notice, as in your example, that local variables can be declared in the block and that they can hide those of the class itself.

You will also come across "instance initializer blocks". They do not have the static keyword and they are called whenever an instance is constructed.
[ May 15, 2004: Message edited by: Barry Gaunt ]
 
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is called a static initializer, it gets executed when the class get loaded. Try searching this forum for more info.
 
Vicken Karaoghlanian
Ranch Hand
Posts: 522
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Damn!!! i should've hit that submit button faster.

Nicely explained thought Barry.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can also see Meeta that these threads are not synchronized, but in this case you got two similar answers, so your confidence of correctness in the responses is hopefully greater than 50%
 
meeta verma
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Barry !!! Got it
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic