• 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 Blocks

 
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't recall seeing this in K&B's book.
Can someone please explain to me how they work and what you would use it for?
Do we need to know it for the exam?
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thought I saw it in K&B but now I can't find anywhere in the book that refers to this.
The main use of static blocks, or static initializers, that I've seen is to load native libraries when the Java class is loaded.
I guess if you know that they run only once when the class is loaded, there doesn't seem to be much else to them. But I beg to be enlightened.
 
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I guess that you may get a question on static blocks. Static blocks are like any other blocks of code, except that they can only access static variables. Secondly static blocks of codes are executed only once for the class. It is executed before the constructor is executed. If a field is static final like static final int a; then the only place other you can intialize the variable is the static intializers other than ofcourse at the time of declaration. This applies only to final variables declared in the class outside of any blocks, methods, etc.
Here is a code that will help you :
 
Ranch Hand
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Damien Howard:
I don't recall seeing this in K&B's book.


I don't either.. As far as I know, if you see static initializers and initializers (without the static) this is the rule:
Static initializers are executed first in the order they appear in the code.
Then, if you have initializers
{
int a = something;
}
they are executed after the static initializers in the order they appear in the code.
I don't know whether it'll appear on the exam, but it's better to get used to it
if someone has something else to add please do so.
cheers
 
Damien Howard
Ranch Hand
Posts: 456
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks everyone for the response.
Can someone show me how you load native libraries using static blocks?
 
Ranch Hand
Posts: 160
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're probably already aware, but there is no way that 'native' question is required knowledge for certification, but by all means if anyone knows...
 
Ranch Hand
Posts: 247
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First, sorry for my approximative english...
As far as I have understood, static blocks are used when static field's initialisation can't be done simply by one expression.
In this case, static block makes the job of a traditionnaly constructor, that can't do nothing for static field. (Indeed, whereas traditionnaly field's initialisation is done via a constructor, it's not possible with static field, because its initialisation must be done before the first use of class
So, an example of useful static block could be the following one:

Hope this helps,
Cyril.
 
Edwin Keeton
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's clearly not on the exam, but since I brought it up, here's how you might load a native library from a static initializer block.
 
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Also a static initializer cannot throw any exception.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm about 99% sure that the exam does not cover static blocks. (Notice I didn't say 100% .) They are worth understanding however outside of your exam prep. My advice would be, (since this IS after all the CERTIFICATION forum, not the JAVA GOD forum), to focus your studies on other areas. I've said it before, I'll say it again:
-inner classes
-threads
-garbage collection
Seem to be the most important and trickiest parts of the exam - they are, IMHO, the best places to spend your study time until you have them totally nailed.
-Bert
p.s. If anybody remembers getting a question with a static block, please speak up. This is something I'd bet $100 on, but I wouldn't bet my life .
 
Anupam Sinha
Ranch Hand
Posts: 1090
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jose Botella:
Also a static initializer cannot throw any exception.


I guess you are saying that the throws clause cannot be used in conjunction with the static intializers. But throw can be used inside a static intializer.
 
reply
    Bookmark Topic Watch Topic
  • New Topic