[Java Silver] About initialization

Instance initialization

-Initialize with ** constructor ** -** Instance variable ** is initialized at ** instance creation **

** Constant (final) ** can also be ** initialized with the constructor ** Uninitialized constants defined in the class can be initialized in the constructor

Example)

public class Main{
   public static void main(String[] args){
       Sample a = new Sample(5);
       System.out.println(a.num);   //Is displayed as 5

   }
}

class Sample{
     final int num;  //The constant field is not initialized at this point
     Sample(int num){  //int num is a local variable with the same name as the instance variable
         this.num = num;  //Initialize constants in constructor
     }
}

Initialize the common processing of the constructor

-Use an initializer (instance initializer) called {} --If there are multiple constructors overloaded, common processing can be performed first in the instance initializer. --The instance initializer runs ** just before it is instantiated **

Example)

public class Main{
   public static void main(String[] args){
       Sample a = new Sample(5);    //"Run Instance Initializer"
       System.out.println(a.num);   //5
       
       Sample b = new Sample();    //"Run Instance Initializer"
       System.out.println(b.num);  //10

   }
}

class Sample{
     final int num;  //The constant field is not initialized at this point
     {
        System.out.println("Run Instance Initializer"); 
     }
     Sample(){
         this.num = 10;
     }
     Sample(int num){  
         this.num = num;  //Initialize constants in constructor
     }
}

Initialize static field

Static fields can be used without instantiating. Therefore, ** cannot be initialized by the constructor **

→ It is necessary to initialize with a static initializer (initializer)

Example)

public class Main{
   public static void main(String[] args){       
       System.out.println(Sample.num);  //100 and output
   }
}

class Sample{
     static final int num;  //Static constants have not been initialized at this point
     static {
        num = 100;  //Initialization of static constants
     }     
}

The order in which the initializers operate

** Initializer-> Instance Initializer-> Constructor **

Example)

public class Main{
   public static void main(String[] args){
       User a = new User();   //Output "Execution of initializer, execution of instance initializer, execution of constructor"
  }

}

class User{
  private static int count;
  static{
      User.count = 0;  //Initialize static variables
    System.out.println("Run initializer");
  }
 
  {
     System.out.println("Run Instance Initializer");
  }

  User(){
     System.out.println("Executing the constructor");
  }
}

Recommended Posts

[Java Silver] About initialization
About inheritance (Java Silver)
[Java Silver] About equals method
[Java] About Java 12 features
Java Silver memo
[Java] About arrays
Something about java
Where about java
About Java features
About Java threads
[Java] About interface
About Java class
About Java arrays
About java inheritance
About interface, java interface
Study Java Silver 1
About List [Java]
About java var
About Java literals
About Java commands
[Java beginner] About initialization of multidimensional array
About Java log output
About Java functional interface
Passed Java SE8 Silver
About class division (Java)
java bronze silver passed
About [Java] [StreamAPI] allMatch ()
Java Silver Study Day 1
Java Silver passing experience
Summarize Java inheritance (Java Silver 8)
About Java method binding
[Java] About anonymous classes
About method splitting (Java)
[java] Java SE 8 Silver Note
About Java Array List
About Java Polymorphism super ()
About Java String class
About Java access modifiers
About Java lambda expressions
About Java entry points
About Java 10 Docker support
Personal summary about Java
Java8 Silver exam memorandum
[Java] About enum type
All about Java programming
About java abstract class
[Java Silver] (Exception handling) About try-catch-finally and try-with-resource statements
A note about Java GC
What I researched about Java 8
About an instance of java
What I researched about Java 6
[Experience] Passed Java SE 8 Silver
[Gradle] About Java plug-in tasks
JAVA Silver qualification exam record
About Java variable declaration statements
What I researched about Java 9
Java Silver Study Method Memo
[Java] About try-catch exception handling
About Java class loader types
Java Silver exam preparation memo
[Java] About String and StringBuilder