This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes static keyword over a block Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static keyword over a block" Watch "static keyword over a block" New topic
Author

static keyword over a block

salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51

HI i never seen to use static keyword over a block. all i have seen to use static only for variables and methods. how can i use staic over a block ? what does it mean? does it behave like ordinary static variables ?
i am getting output 20. i want to know about the use of static keyword over a block . anybody can explain how does it work ? and why the output is 20 ?
Jeff Langr
author
Ranch Hand

Joined: May 14, 2003
Posts: 762
It's a static initialization block; it gets called when the class is first loaded. It runs before static field level initializers. It can contain more than one statement. If you only have a trivial field level initialization (as in your example), you would not normally use a static initialization block.


Books: Agile Java, Modern C++ Programming with TDD, Essential Java Style, Agile in a Flash. Contributor, Clean Code.
Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
take a look at this...

The output is:
Static code i = 5
Main code i = 6


hello everyone i'm new here.
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
static means it will create only one copy. i think the same rule is also applicable to the static initializer blocks.
i.e every object will get the same block of code (static) and if any change occurs then that would be for all.
salvador rcn
Ranch Hand

Joined: Feb 18, 2004
Posts: 51
hi Ayukawa Madoka , your code is not working. r u sure ?
i copied and pasted, it is giving me compilation error.
of course the error is quite reasonable bcoz a java source code can not have two or more public classes ( your code has two public class ).
however, your example is good to suport the mechanism of static block operation.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
I've used this kind of static block to initialize a factory with a hashmap of keywords and classnames or similar lookups. I thought it saved my neck the first time I ran across it.


A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Ayukawa Madoka
Greenhorn

Joined: Feb 28, 2004
Posts: 27
Originally posted by salvador rc:
hi Ayukawa Madoka , your code is not working. r u sure ?
i copied and pasted, it is giving me compilation error.
of course the error is quite reasonable bcoz a java source code can not have two or more public classes ( your code has two public class ).
however, your example is good to suport the mechanism of static block operation.

First error was a typo, sorry. it's "String" and not "Sting".
Second error...
cut and paste the "Test" class on a new template and save it as Test.java

compile and execute it. it should work now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static keyword over a block
 
Similar Threads
Static initializer
from mock exam qs.
Doubt in static...
static block
static block