Java – what does this’ static ‘mean and why
•
Java
public class tt {
public class tt { static{ System.out.println("class tt"); } }
This is the first time I have met it. I want to know what it is and what it is used for
Solution
It is the static initializer of this class When the class is loaded, the static initializer runs It's like a constructor, but for classes, not individual objects
Multiple static initializers can appear in classes or in direct initializers of static variables These will be combined into one initializer in the declared order For example, whenever a class is loaded, the following will print "foo" to stdout (usually once per application)
public class Foo { static String a; static { a = "foo"; } static String b = a; static { System.println(b); } }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码