0

Possible Duplicate:
Static initializer in Java

I wondering what this static something (sorry it's my first time encountering this) does for a class or what is its purpose.

    public class SomeClass{

           //this is a static field
           private static String someStaticStringField;
           ... //other static fields

           //what is this?
           static{
                 log.debug("Loading config file");
                 try{
                        Class cls = Class.forName("package.ClassName");  
                        properties = new Properties();

                        ...

                 } catch(Exception e){
                        log.error("Error in loading config file");
                 } 


           }

           //this is a static method
           public static String getSomeStaticStringField(){
                  return someStaticStringField;
           }


    }

On general, what does that static{} do and its purpose for the Class? What is the right term to call it? Is it a Class field, a method, or something else? Thanks

Community
  • 1
  • 1
Frank Smith
  • 1,717
  • 4
  • 18
  • 32
  • 1
    See also [the Java Tutorial](http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html) and the section on static intialization blocks. – assylias Aug 24 '12 at 08:01
  • the code after the static field, is a static initializer block. See here: http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html more details. – dan Aug 24 '12 at 08:03

1 Answers1

4

This is called the static initializer; it is run when the class is first referenced (just like any initialization for static variables, except this allows better initialization as you have a whole block). It is not a method nor a field

Alex Coleman
  • 7,216
  • 1
  • 22
  • 31