Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static keyword over a block

 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 799
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a look at this...

The output is:
Static code i = 5
Main code i = 6
 
salvador rcn
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Ayukawa Madoka
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic