0
What is static initializer
2 Réponses
+ 2
A static initializer can also be called a class initializer.
It is ran by JVM when the class is loaded, and can be used to initialize or call any static member. Unlike the Instance initializer, this will only be called once since it is static.
0
Well, if you look at the Math class, there are many static methods. You don't want to create a new Math instance every time you use for example the pow() function. If a method or variable is static, it belongs to the class and not to an object. For example, if there's a method incrementBy(int x)
which is called in the constructor of the class which contains that method with the argument 2 and the static variable y = 0, and you create three objects of that class, y will be 6. If a variable isn't static, you call it using instanceName.variable while a static one is called by this.variable or className.variable.