initialization « Static « Java Class Q&A

Java Class Q&A
1.abstract class
2.Base class
3.class hierarchy
4.class name
5.class version
14.equal method
15.extend Class
19.inner class
21.main class
26.object reference
28.parent class
31.Private Field
35.Static Class
39.Wrapper Class
Java Class Q&A » Static » initialization 

1. static variable initialization java

how to initialize a private static member of a class in java. trying the following:

public class A {
   private static B b = null;
   public A() {

2. Exception in static initialization block

Why doesn't Java allow to throw an exception from static initialization block? What was the reason behind this design decision? Any help would be greatly appreciated. Thanks.

3. Java - Can final variables be initialized in static initialization block?

Upto my theoretical knowledge, static variables can be initialized in static initialization block. But when I tried to implement the above (static variables that are final too), I got an error as ...

4. 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 ...

5. Java initialization order issue, static vs instance fields

The program below prints:

my name is:null

my name is:null

Someclass static init
AFAIK when a class is first loaded static blocks and fields are always initialized first, instance blocks and fields second. Therefore variables ...

6. Are there any guarantees in JLS about order of execution static initialization blocks?

I wonder if it's reliable to use a construction like:

private static final Map<String, String> engMessages;
private static final Map<String, String> rusMessages;

static {
    engMessages = new HashMap<String, String> () {{

7. Static Final initialization of fields in java.Need some help !(an Interview question)

public class Main {
 static final int alex=getc();
 static final int alex1=Integer.parseInt("10");
 static final int alex2=getc();

public static int getc(){
    return alex1;


public static void main(String[] args) {


8. Breaking from Static Initialization Block in Java

I have a static initialization block. It sets up logging to a file. If something goes wrong, I simply want to break out of the static block. Is this possible? I ...

9. Java Cyclomatic Enum initialization - what happens & why does it happen?

Possible Duplicate:
Java Enums: Two enum types, each containing references to each other?
In our code we got some weird NPE's concerning Enums. When I searched, ...

10. java unload static fields

I have a java class that uses complex static fields which need special operations as close() so that they are safely cleaned by GC. For the initialization of static fields I use ...

11. static member initialization

Hi, Can somebody explain difference between methods to initilize static fields of a class. When is the order of initialization important and which method to prefer if class A is a Java Bean to be invoked from a JSP class A{ public static String HELLO = HELLO WORLD; //declare and initialize } class A{ public static String HELLO; public A(){ HELLO ...

12. when static initialization occurs

consider, if you will, the following code: public final class Team { public static Team team1=new Team("Loonies"); public static Team team2=new Team("Dingbats"); public static Team team3=new Team("Simpletons"); public static Team team4=new Team("Wonkers"); public static Team team5=new Team("Plonkers"); public static Team melee=new Team("Melee"); public static final List teams(){ List list=new ArrayList(); list.add(team1); list.add(team2); list.add(team3); list.add(team4); list.add(team5); list.add(melee); System.out.println(list); return list; } } ...

13. Illegal use of a qualified name in static initialization block

It could be a straightforward rule somewhere, but it's not jumping out at me. On the other hand I can't think of any reason why you should ever need to use a qualified name when assigning a static final. This can only ever be done from within the class that declares the final, and so the field is already accessible without ...

14. Static variable initialization

Hi My understanding is that all static variables and static blocks shall be initialized/executed before anyone accesses the class (static method or object creation). Below code is not working as expected, please help me in understanding where I am wrong. public class StaticTest { private static StaticTest myInstance = new StaticTest(); static String str = null; static int i =0; private ...

15. Static Initialization

class Tag { Tag (int marker) { System.out.println("Tag(" + marker + ")"); } void f() { System.out.println("f()"); } } class Card { Tag t1 = new Tag(1); Card() { System.out.println("Card()"); t3 = new Tag(33); } static Tag t3 = new Tag(3); } public class OrderOfInitialization { public static void main (String[] args) { System.out.println("Inside main method"); Card.t3.f(); new Card(); new Card(); ...

16. Static Initialization Block

Hi, Here are 2 cases when I often use static initializers. Other programmers could probably add more scenarios. 1. When I'd like to initialize some static members, but the initial value is too complex to be calculated at a simple declaration lines. For example: class MyClass{ // this is simple, so it doesn't require static initializer: static String msg = "hello"; ...

17. Static initialization block?????

In general , when there are more than one static blocks define, the order of execution depends upon the order in which they defined, i.e , the first defined blocks execute then second block in order of declaration .. So i thought when you are loading class B, its executes it parent class (A) , static block first then its derived ...

18. static and initialization block

19. can we initialise static variables in static initialization block ?

Hi JavaAchievers, im trying to compile the following code, class Chap3Ex7 { static { static int i=15; } { int i=12; } public static void main(String[] args) { Chap3Ex7 ch = new Chap3Ex7(); } } im getting something related to : illegal start of expression static int i=15; ^ my question is can't we use static variables in static block ...

20. Facing null pointer exception in static initialization

What on earth i am doing wrong /* * To change this template, choose Tools | Templates * and open the template in the editor. */ package constants; import; import; import java.util.HashMap; import java.util.Map; /** * * @author sumit kumar */ public class ConstantObject implements Serializable{ private final String name; public static final ConstantObject canada = new ConstantObject("CANADA"); public ...

21. Static Initialization block

22. About static variable initialization

23. Initialization of the static variable

24. static field initialization

I see what you're saying. I should amend my statement to say that it's safe so long as the "b" field is not changed after class initialization (it's safe if it's not changed because of the synchronization that occurs on the Class object). However, it does not need to be final to be safe if that is true, although it might ...

26. static initialization

Hello Guys! Im curious about the following behavior below. public class Car { static String name = "NULL"; static { name = "MyCar"; } public static void main(String[] args) { System.out.println(; } } Here, any doubts. I receive "MyCar" as out, okay! But, when I change the code for the following... public class Car { static { name = "MyCar"; } ...

27. Static Initialization

First read throughly the posts and reply Sounds like reasonable advice. So, what was the meaning of that first word in reply one again? It has been suggested that truely static data be initialised in a static block. And that data that is different for each - well, instance - of the Class's use be initialsed in a constructor. And I ...  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.