Initialization of static data members. Static block. Arrays initialization
Contents
- 1. Initialization of static data members. How are static data members initialized?
- 2. An example of initializing a static members of the class. Initializing a static data member using the method
- 3. What is the order of initialization if both static and non-static members of the class are declared in the class?
- 4. What is a static block? General form
- 5. Example of using a static block
- 6. How many times is the static block called? In what cases is the static block called?
- 7. Initialize arrays in the class. How do I initialize an array with values in a class?
- 8. Example of initializing one-dimensional arrays in a class
- 9. Example of initializing a two-dimensional array in a class
- Related topics
Search other websites:
1. Initialization of static data members. How are static data members initialized?
If a static member is declared in the class, it always exists in the same instance, regardless of the number of objects that the class creates. A static data member can be declared as a member of the class data:
class MyClass { static int d; // a static data member of the class static float x; // a static data member of the class ... }
If a static data member of a primitive data type is not initialized in the class, it is assigned a default value. If a static data member is a reference to an object, then it is set to null.
The following are the default values that are assigned to static data members in the absence of explicit initialization
static int i; // i = 0 static boolean b; // b = false static float f; // f = 0.0 static double d; // d = 0.0 static char c; // c = character with code 0 static CDate objDate; // objDate = null
If you need to explicitly initialize a static member of the data at the point of its declaration, then it is the same way as for the non-static data member:
class MyClass { static int d = 25; // initializing a static member with a specific value static float x = 108.35; ... }
More detailed work with static data members in Java is covered in the topic:
2. An example of initializing a static members of the class. Initializing a static data member using the method
The example declares two classes with the names CDate and CStaticData. In the CStaticData class, static members of the class data are initialized in the same way as non-static members of the class data.
// initializing static members of the class public class CStaticData { static int i = 2588; static boolean b = true; static float f = 3.23f; static double d = -10.2093; // initializing a static object (instance) of a class static CDate objDate = new CDate(); // initializing a static member of a class using the GetPi() method static double Pi = GetPi(); // The GetPi() method of the class can also be static static double GetPi() { return 3.1415; } public static void main(String[] args) { CStaticData obj = new CStaticData(); obj.b = false; CStaticData.d = -10.223; } }
As you can see from the example, to initialize a static member of the Pi data using the GetPi() method, this method can also be declared as static.
3. What is the order of initialization if both static and non-static members of the class are declared in the class?
If static and non-static members of the class are declared in the class, static members of the class are initialized first. After this, non-static members of the class are initialized.
Example. The class CDataInit is declared, in which static and non-static members of the class data are initialized in an arbitrary order.
public class CDataInit { // internal functions, which are used to initialize int GetInt(String s) { System.out.println("Non-static initialization of " + s); return 100; } static int GetIntStatic(String s) { System.out.println("Static initialization of " + s); return 5; } // the variables that need to be initialized - announced in random order int i1 = GetInt("i1"); static int si1 = GetIntStatic("si1"); int i2 = GetInt("i2"); int i3 = GetInt("i3"); static int si2 = GetIntStatic("si2"); int i4 = GetInt("i4"); static int si3 = GetIntStatic("si3"); public static void main(String[] args) { CDataInit obj = new CDataInit(); } }
After the CDataInit object is declared, the following lines are output:
Static initialization of si1 Static initialization of si2 Static initialization of si3 Non-static initialization of i1 Non-static initialization of i2 Non-static initialization of i3 Non-static initialization of i4
As you can see from the above result, the static data members are initialized first.
4. What is a static block? General form
A static block is a special construction that helps group several actions intended to initialize static data members.
The general form of a static block:
static { // operations that initialize static members of the class // ... }
In the class, the static block looks something like this:
class ClassName { ... static { // initialization of static class members // ... } ... }
5. Example of using a static block
In the CDataInit class, a static block is declared in which the static variables a, b, c, d are initialized.
public class CDataInit { static int a, b, c, d; // initialization with static block static { a = 1; b = 2; c = 3; d = 4; } public static void main(String[] args) { CDataInit obj = new CDataInit(); int t; // checking t = obj.a; // t = 1 t = obj.b; // t = 2 t = obj.c; // t = 3 t = obj.d; // t = 4 } }
6. How many times is the static block called? In what cases is the static block called?
A static block is called only once in one of two cases:
- when you create a first object of this class;
- when first accessing static members of this class.
Example. Let the class CDataInit be given, which contains the declaration of static members with the names a, b, c, d.
public class CDataInit { static int a, b, c, d; // initialization with static block static { a = InitMethod("a = "); b = InitMethod("b = "); c = InitMethod("c = "); d = InitMethod("d = "); } static int InitMethod(String s) { System.out.println(s + "100"); return 100; } public static void main(String[] args) { // object creation - call a static block CDataInit obj = new CDataInit(); CDataInit obj2 = new CDataInit(); // The second time the static block is not called } }
As a result of this code, the result will be displayed:
a = 100 b = 100 c = 100 d = 100
As you can see, the main() function creates two objects, and the initialized data is output only once. This confirms the rule, which states: static data members are initialized in the class only once.
If in the main() function, instead of the string
CDataInit obj = new CDataInit();
enter the reference code for the static variable, for example
CDataInit.a = 25;
then a static initialization block will also be called, but only once.
7. Initialize arrays in the class. How do I initialize an array with values in a class?
In Java, an array in a class can be initialized in one of two ways:
- direct assignment of array values. In this case, in the curly brackets { } is given a set of values which are assigned to the array;
- assigning a reference to another array. In this case, a reference to another array is assigned using the assignment operator ‘=’.
8. Example of initializing one-dimensional arrays in a class
A CInitArray class is declared, in which arrays a, x and references to them with the names b, y are declared.
public class CInitArray { // initializing an array in a class // way 1 - immediate initialization with values int[] a = { 1, 2, 4, 9, -8, 13, 7 }; // array has 7 elements double x[] = { -1.33, 2.905, 1.308, 10.301, 8 }; // array has 5 elements // way 2 - assignment of reference int[] b = a; // array 'b' refers to the array 'a' double[] y = x; // array 'y' refers to the array 'x' public static void main(String[] args) { CInitArray M = new CInitArray(); int len; // length of arrays len = M.a.length; // len = 7 len = M.x.length; // len = 5 // checking specific values of arrays int d; double f; d = M.a[3]; // d = 9 f = M.x[2]; // f = 1.308 d = M.b[3]; // d = 9 len = M.b.length; // len = 7 f = M.y[0]; // f = -1.33 len = M.y.length; // len = 5 // a change in the cell of array 'a' results in a change in the cell of array 'b' and vice versa M.a[2] = 100; d = M.b[2]; // d = 100 M.b[0] = -11; d = M.a[0]; // d = -11 // a change in the cell of array 'x' results in a change in the cell of array 'y' and vice versa M.x[3] = 1.75; f = M.y[3]; // f = 1.75 M.y[1] = -3.22; f = M.x[1]; // f = -3.22 } }
Arrays a, x are initialized with initial values using curly braces. After that, the length of the arrays is unchanged.
Arrays of class b and y are initialized with values that are in arrays a, x. Rather, arrays b and y refer to the cells of arrays a, x.
In the main() function, the length of each array and the values of the elements of the array are tested. The length of each array can be determined using the length property.
9. Example of initializing a two-dimensional array in a class
The initialization of a two-dimensional array is the same as one-dimensional.
public class CInitArray { // initializing a two-dimensional array in a class // way 1 - immediate initialization with values // a two-dimensional array aa of size 4*3 is declared int[][] aa = { { 1, 2, 5 }, { 3, 8, 9 }, { 4, 11, -7 }, { 2, 2, 0 } }; // way 2 - the assignment of reference int[][] bb = aa; // bb refers to aa public static void main(String[] args) { CInitArray M = new CInitArray(); int len, d; // checking the length of arrays len = M.aa.length; // len = 4 - number of lines len = M.aa[0].length; // len = 3 - number of columns in a row with index 0 // checking values d = M.aa[0][2]; // d = 5 // a change in the cell of array 'a' results in a change in the cell of array 'b' and vice versa M.aa[0][2] = 13; d = M.bb[0][2]; // d = 13 M.bb[1][0] = 25; d = M.aa[1][0]; // d = 25 } }
Related topics
- Initialize variables in class methods. Initialization of data members of the class
- The use of classes in Java programs. Class and object definition. Examples