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 ?
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.
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.
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.
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
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.
hello everyone i'm new here.
A lot of people cry when they cut onions. The trick is not to form an emotional bond. This tiny ad told me: