Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java syntax question

Tags:

java

syntax

What does

static{
    //something
}

declared inside a class definition body mean?

public class A extends B{
     static {
          C.register(new C(A.class,
                    (byte) D.x.getCode()) {
                         public DataSerializable newInstance() {
                              return new A();
                         }
                    }
               );
     }
 }
like image 266
Luchian Grigore Avatar asked Jul 28 '11 13:07

Luchian Grigore


People also ask

What does a question mark (?) Designate in Java?

In generic code, the question mark (?), called the wildcard, represents an unknown type. The wildcard can be used in a variety of situations: as the type of a parameter, field, or local variable; sometimes as a return type (though it is better programming practice to be more specific).

What is the other name for a question mark colon (? :) operator in Java?

This is called the conditional expression or the question mark-colon operator.

What is ?: Called in Java?

?: is a Ternary Java Operator.

What is && and || in Java?

The && and || operators perform Conditional-AND and Conditional-OR operations on two boolean expressions. These operators exhibit "short-circuiting" behavior, which means that the second operand is evaluated only if needed.


1 Answers

The static block is called a "static initialization block." It is very similar to a regular constructor except that it can only initialize static variables.

I have found it to be useful when initialization of some static variable may throw an exception that you would like to handle or at least log. It is especially useful in the initialization of static final variables.

You may read more about static initialization blocks here: Initializing Fields

like image 188
Jack Edmonds Avatar answered Oct 20 '22 01:10

Jack Edmonds