A static block is a normal block of code enclosed in braces, { }, and preceded by the static keyword.
Questions tagged [static-block]
166 questions
                    
                    308
                    
            votes
                
                14 answers
            
        Static Initialization Blocks
As far as I understood the "static initialization block" is used to set values of static field if it cannot be done in one line. 
But I do not understand why we need a special block for that. For example we declare a field as static (without a value…
         
    
    
        Roman
        
- 124,451
- 167
- 349
- 456
                    98
                    
            votes
                
                11 answers
            
        In what order do static blocks and initialization blocks execute when using inheritance?
I have two classes Parent and Child
public class Parent {    
    public Parent() {
        System.out.println("Parent Constructor");
    }    
    static {
        System.out.println("Parent static block");    
    }    
    {
       …
         
    
    
        CKR666
        
- 1,497
- 1
- 13
- 10
                    87
                    
            votes
                
                2 answers
            
        Static block vs. initializer block in Java?
Possible Duplicate:
Static Initialization Blocks 
Consider the following code:
public class Test {
    {
        System.out.println("Empty block");
    }
    static {
        System.out.println("Static block");
    }
    public static void…
         
    
    
        Anshu
        
- 7,783
- 5
- 31
- 41
                    79
                    
            votes
                
                9 answers
            
        When is the static block of a class executed?
I have 2 jars, let's call them a.jar and b.jar.
b.jar depends on a.jar.
In a.jar, I defined a class, let's call it StaticClass. In the StaticClass, I defined a static block, calling a method named "init" : 
public class StaticClass {
  static {
   …
         
    
    
        Leon
        
- 8,151
- 11
- 45
- 51
                    52
                    
            votes
                
                10 answers
            
        Mocking Static Blocks in Java
My motto for Java is "just because Java has static blocks, it doesn't mean that you should be using them." Jokes aside, there are a lot of tricks in Java that make testing a nightmare. Two of the most I hate are Anonymous Classes and Static Blocks.…
         
    
    
        Cem Catikkas
        
- 7,171
- 4
- 29
- 33
                    44
                    
            votes
                
                4 answers
            
        Enums - static and instance blocks
I had learned that in Java the static block gets executed when the class is initialized and instance block get executed before the construction of each instance of the class . I had always seen the static block to execute before the instance block .…
         
    
    
        AllTooSir
        
- 48,828
- 16
- 130
- 164
                    41
                    
            votes
                
                7 answers
            
        What's the C++ idiom equivalent to the Java static block?
I have a class with some static members, and I want to run some code to initialize them (suppose this code cannot be converted into a simple expression). In Java, I would just do
class MyClass {
    static int field1;
    static int field2;
   …
         
    
    
        einpoklum
        
- 118,144
- 57
- 340
- 684
                    35
                    
            votes
                
                5 answers
            
        Behavior of static blocks with inheritance
I am trying to use static blocks like this:
I have a base class called Base.java
public class Base {
    static public int myVar;
}
And a derived class Derived.java:
public class Derived extends Base {
    static
    {
        Base.myVar = 10;
 …
         
    
    
        Asha
        
- 11,002
- 6
- 44
- 66
                    32
                    
            votes
                
                8 answers
            
        Value of static variable not changed even after initializing the child class in Java
When I invoke the static variable y by using Checks.y (Checks being a subclass), the static block is not executed and the value of y doesn't get updated.
class Par {
    static int y = 4;
}
class Checks extends Par {
    static {
        y = 5;
   …
         
    
    
        tusharRawat
        
- 719
- 10
- 24
                    32
                    
            votes
                
                4 answers
            
        How to initialize ThreadLocal objects in Java
I'm having an issue where I'm creating a ThreadLocal and initializing it with new ThreadLocal . The problem is, I really conceptually just want a persistent list that lasts the life of the thread, but I don't know if there's a way to initialize…
         
    
    
        B T
        
- 57,525
- 34
- 189
- 207
                    28
                    
            votes
                
                1 answer
            
        What is Scala equivalent of Java's static block?
What is Scala equivalent of Java's static block ?
         
    
    
        missingfaktor
        
- 90,905
- 62
- 285
- 365
                    23
                    
            votes
                
                1 answer
            
        Why can't we set the value of static final variable in static block through class name
For example, consider code snap below:
public static final int a;
public static final int b;
static {
    a = 8;       // it's working
    Test.b = 10; // compilation error Test.b cannot be assigned. 
}
Why can't we use Test.b = 10; inside a…
         
    
    
        Snehal Patel
        
- 1,282
- 2
- 11
- 25
                    18
                    
            votes
                
                3 answers
            
        What does "When a Class is loaded" actually mean?
It is said that static blocks in java run only once when that class is loaded. But what does it actually mean? At which point is a class loaded by JVM (Java Virtual Machine)?
Is it when the main method in that class is called? And is it that all the…
         
    
    
        whitehat
        
- 2,381
- 8
- 34
- 47
                    12
                    
            votes
                
                4 answers
            
        Is a Java static block equivalent to a C# static constructor?
What is the real difference between a C# static constructor and a Java static block?
They both must be parameterless.
They are both called only once, when the related class is first used.
Am I missing something, or are they the same thing, just with…
         
    
    
        Mackenzie
        
- 1,897
- 1
- 19
- 25
                    11
                    
            votes
                
                4 answers
            
        How to pass parameter with block form contents from cms pages in magento
I want to pass a variable with the block code like of JSON type in magento,
{{block type="multibanners/multibanners" category_id="9" name="multibanners" alias="multibanners" template="multibanners/multibanners.phtml"}}
from cms pages content area ,…
         
    
    
        Mahmood Rehman
        
- 4,303
- 7
- 39
- 76