3

I have the following class:

public abstract class A()
{
   public static final SomeString = null;

   static()
   {
       SomeString = "aaa";
   }
}

When is this static method invoked and how?

What is the purpose in creating such a static method (without name / return type)?

DavidPostill
  • 7,734
  • 9
  • 41
  • 60
akaspi
  • 275
  • 1
  • 3
  • 12

4 Answers4

7

That is not a method, it's a static initialization block, and your syntax is wrong

public abstract class A()
{
   public static String SomeString = null;

   static
   {
       SomeString = "aaa";
   }
}

The easiest way of initializing fields (static or instance) in Java at the time of their declaration is simply by providing a compile time constant value of a compatible data type. For example:

public class InitializationWithConstants{

public static int staticIntField = 100;
private boolean instanceBoolField = true;

}

This type of initialization has its limitation due to its simplicity and it can not support initialization based even on some moderately complex logic - like initializing only selected elements of a complex array using some logic in a for loop.

Here comes the need for static initialization blocks and initializer blocks for initializing static and instance fields, respectively.

It's a normal block of code enclosed within a pair of braces and preceded by a 'static' keyword. These blocks can be anywhere in the class definition where we can have a field or a method. The Java runtime guarantees that all the static initialization blocks are called in the order in which they appear in the source code and this happens while loading of the class in the memory.

public class InitializationWithStaticInitBlock{

public static int staticIntField;
private boolean instanceBoolField = true;

static{
 //compute the value of an int variable 'x'
 staticIntField = x;
}
}

Since static initialization blocks are actually code-blocks so they will allow us to initialize even those static fields which require some logical processing to be done for them to get their initial values.

Abimaran Kugathasan
  • 31,165
  • 11
  • 75
  • 105
0

Your syntax is incorrect; it should be:

public abstract class A()
{
    public static final String SomeString;

    static
    {
        SomeString = "aaa";
    }
}

The static block allows static variables to be initialised when the class is loaded when that initialisation is more complication than simply = something;.

Reference

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

Syntax aside, you're looking at a static initializer block. They're mentioned here.

Essentially, a static initializer block is a piece of code that executes when a class is loaded, and can be used to initialize static fields in the class.

Example:

public abstract class A
{
    public static final String SomeString;

    static
    {
        System.out.println("static{}");    
        SomeString = "aaa";
    }    

    public static void main(String[]args)
    {
        System.out.println("main");    
    }
}

Output:

static{}
main
Vlad
  • 18,195
  • 4
  • 41
  • 71
0

yeas it's not a method. it is static block and i'st evaluated once when the owner class is loaded.
u can use it for initialize static variable dynamically ...

Sumit Singh
  • 15,743
  • 6
  • 59
  • 89