Java estático: ¿método o campo o no? [duplicar]
Frecuentes
Visto 146 veces
0
Posible duplicado:
Inicializador estático en 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
1 Respuestas
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
Respondido 24 ago 12, 09:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java static or haz tu propia pregunta.
Vea también el tutorial de Java and the section on static intialization blocks. - assylias
the code after the static field, is a static initializer block. See here: docs.oracle.com/javase/tutorial/java/javaOO/initial.html más detalles. - dan