لوسمحتم قريت بكتاب شي اسمه static initializer بالجافا وبصراحه قريت فقره من الكتاب ماعرف شنو الشرح من الفقره ممكن اي احد يعرف عن static initializer يعطيني اي شرح عنه وهذي الفقره الي قريت فيها مع مثال عليه.
اقتباسAs you’ve already learned, instance variables are normally initialized insideconstructors. But what about class variables? How do we initialize them?
By the static initializer of a class is a JAVA routine that gets executed when the
class is loaded before the class gets used by the program. Static initializers
have weird syntax. As [8] explains it, a static initializer doesn’t take input
and doesn’t produce output, and there is really no need to name it since
we won’t be calling it. What is left from the class method declaration
is the word static and the curly brackets. And that’s exactly how you
define class initializers.
Let’s take an example. Here’s a class that needs to maintain an array
of powers of two. See how it initializes the array using a static initializer:
1 class Dummy {
2 final static int MAX = 10 ;
3 static long[] powers ;
4 static {
5 powers = new long[MAX] ;
6 powers[0] = 1 ;
7 for (int i = 1 ; i < MAX ; i++)
8 powers = powers[i-1]*2 ;
9 }
10 }
يعني بالمختصر شنو فايدة static initializer
