Teachnique
      CourseRoadmaps
      Login

      OverviewHistoryFeaturesJava 8 - New Featuresvs C++Virtual Machine(JVM)JDK vs JRE vs JVMHello World ProgramEnvironment SetupBasic SyntaxVariable TypesData TypesType CastingUnicode SystemBasic OperatorsCommentsStreamsNew Date-Time API

      Loop ControlDecision Makingif-else Statementswitch statementfor loopfor each Loopwhile Loopdo...while Loopbreak Statementcontinue Statement

      OOPs (Object-Oriented Programming) ConceptsObject and ClassesClass AttributesClass MethodsMethodsVariable ScopesConstructorsAccess ModifiersInheritanceAggregationPolymorphismOverridingMethod OverloadingDynamic BindingStatic BindingInstance Initializer BlockAbstractionEncapsulationInterfacesPackagesInner classesStatic ClassesAnonymous ClassesSingleton ClassWrapper ClassesEnum Class

      Number ClassBoolean classCharacter ClassArraysMath Class

      File ClassCreating FilesWrite To FilesReading FileDelete FilesDirectory OperationsFiles and I/O

      ExceptionsTry Catch BlockTry with ResourcesMultiple Catch BlocksNested Try BlockFinally BlockThrows and Throw | Throw an ExceptionException PropagationBuilt-in ExceptionsCustom Exception

      MultithreadingThread Life CycleCreating a ThreadStarting a ThreadJoining ThreadsNaming a Thread with ExamplesScheduling Threads with ExamplesThread PoolsMain ThreadThread PriorityDaemon ThreadThreadGroup ClassJVM Shutdown Hook

      Thread SynchronizationBlock SynchronizationStatic SynchronizationInter Thread CommunicationThread DeadlockInterrupting ThreadThread ControlReentrant Monitor

      NetworkingSocket ProgrammingURL ProcessingURL ClassURLConnection ClassHttpURLConnection ClassSocket Class with ExamplesGenerics

      Collections FrameworkCollection Interface

      List InterfaceArrayList Class

      Queue InterfaceArrayDeque Class

      Map InterfaceSortedMap Interface

      Set InterfaceSortedSet Interface

      Data Structures Enumeration Interface BitSet Class

      How to Use Iterator?How to Use Comparator?How to Use Comparable?

      RecursionRegular ExpressionsSerializationString ClassJava Arrays - Class

      Feedback

      Submit request if you have any questions.

      Course
      Instance Initializer Block

      Java Tutorial

      This Java tutorial is tailored for newcomers, offering a journey from basic principles to complex Java programming techniques. Completing this tutorial equips you with a solid understanding of Java, preparing you for advanced learning. You'll emerge ready to tackle the challenges of becoming a top-tier software engineer, with the skills to innovate and excel in the vast world of software development.

      Instance Initializer Block

      Java Instance Initializer Block

      An instance initializer block is a block of code that is declared inside a class to initialize the instance data members. Instance Initializer block is executed once for each object and can be used to set initial values for instance variables.
      The instance initializer block is similar to the Java constructor but its execution and uses are are different.

      Java Instance Initializer Block Example

      This example demonstrates instance initializer block in Java:
      public class Tester {
      public int a;
      { a = 10; }
      }

      Characteristics of Instance Initializer Block

      • Instance initializer block is called once an object is created.
      • Instance initializer block is called before any constructor of an object is invoked.
      • In case of child class, Instance initializer block will be called after super class constructor call.
      • Instance initializer block can be used to execute multiple statements.
      • Instance initializer block is generally used to instantiate multiple values fields like arrays.

      Use of Instance Initializer Block

      The following are the uses of instance initializer block in Java:
      • To initialize the instance variable.
      • To initialize the resources used in the code.
      • To perform the dynamic initialization of the instance variables.
      • To use the common initialization logic for multiple constructors.

      Java Instance Initializer Block: More Examples

      Example 1: Demonstrating What Invokes First, Instance Initializer Block or Constructor

      In this example, we've shown that instance initializer block is getting executed before the object constructor. Both instance initializer block and constructor are invoked when object is created using new operator.
      package com.tutorialspoint;
      public class Tester {
      {
      System.out.println("Inside instance initializer block");
      }
      Tester(){
      System.out.println("Inside constructor");
      }
      public static void main(String[] arguments) {
      Tester test = new Tester();
      }
      }

      Output

      60
      150

      Example 2: Demonstrating Whether Constructor Overrides Instance Initializer Block

      In this example, we've shown that a value initialized in instance initializer block is getting overriden by the object constructor. Both instance initializer block and constructor are invoked when object is created using new operator.
      package com.tutorialspoint;
      public class Tester {
      int a;
      {
      System.out.println("Inside instance initializer block");
      a = 10;
      }
      Tester(){
      System.out.println("Inside constructor");
      a = 20;
      }
      public static void main(String[] arguments) {
      Tester test = new Tester();
      System.out.println("Value of a: " + a);
      }
      }

      Output

      Inside instance initializer block
      Inside constructor
      Value of a: 20

      Example 3: Instance Initializer Block and Super Constructor

      In this example, we've shown that a super constructor is invoked before child instance initializer block. We've created a SuperTester class and Tester class extends this class. In main() method, we're printing the value of instance variable. In output, you can verify the order of blocks invoked. First super constructor is invoked. Then child instance initializer is invoked which initializes an instance variable and then constructor of child class is invoked.
      package com.tutorialspoint;
      class SuperTester{
      SuperTester(){
      System.out.println("Inside super constructor");
      }
      }
      
      public class Tester extends SuperTester {
      int a;
      {
      System.out.println("Inside instance initializer block");
      a = 10;
      }
      
      Tester(){
      System.out.println("Inside constructor");
      }
      
      public static void main(String[] arguments) {
      Tester test = new Tester();
      System.out.println("Value of a: " + test.a);
      }
      }

      Output

      Inside super constructor
      Inside instance initializer block
      Inside constructor
      Value of a: 10