• 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
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

static keyword over a block

 
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
author
Posts: 799
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me:
Master Gardener Program
https://coderanch.com/t/771761/Master-Gardener-Program
reply
    Bookmark Topic Watch Topic
  • New Topic