in Technology by

What should be the execution order, if a class has a method, static block, instance block, and constructor, as shown below?

public class First_C {  

      public void myMethod()   

    {  

    System.out.println("Method");  

    }  

      

    {  

    System.out.println(" Instance Block");  

    }  

          

    public void First_C()  

    {  

    System.out.println("Constructor ");  

    }  

    static {  

        System.out.println("static block");  

    }  

    public static void main(String[] args) {  

    First_C c = new First_C();  

    c.First_C();  

    c.myMethod();  

  }  

}   

a) Instance block, method, static block, and constructor

b) Method, constructor, instance block, and static block

c) Static block, method, instance block, and constructor

d) Static block, instance block, constructor, and method

🔗Reference: stackoverflow.com

🔗Source: Java Interview Questions and Answers

1 Answer

0 votes
by

Answer: (d) Static block, instance block, constructor, and method

Explanation: The order of execution is:

  1. The static block will execute whenever the class is loaded by JVM.
  2. Instance block will execute whenever an object is created, and they are invoked before the constructors. For example, if there are two objects, the instance block will execute two times for each object.
  3. The constructor will execute after the instance block, and it also execute every time the object is created.
  4. A method is always executed at the end.

Hence, the correct answer is an option (d).

Related questions

0 votes
    Can abstract keyword be used with constructor, Initialization Block, Instance Initialization and Static Initialization ... Java Select the correct answer from above options...
asked Feb 23, 2022 in Education by JackTerrance
0 votes
    I am trying to understand Static methods in java. I have done a basic python before, so I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I am trying to understand Static methods in java. I have done a basic python before, so I am ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 13, 2022 in Education by JackTerrance
0 votes
    In order to restrict a variable of a class from inheriting to subclass, how variable should be declared? ( ... questions and answers pdf, java interview questions for beginners...
asked Oct 25, 2021 in Education by JackTerrance
0 votes
    What is the static block?...
asked Nov 19, 2020 in Education by Editorial Staff
0 votes
    How will you call a static method of an interface in a class in Java8?...
asked Nov 8, 2020 in Education by Editorial Staff
0 votes
    Can servlet class declare constructor with ServletConfig object as an argument? (a) True (b) False I have been ... & Servlet of Java Select the correct answer from above options...
asked Feb 22, 2022 in Education by JackTerrance
0 votes
0 votes
    If we need to inherit from a base class and want to pass something from the constructor of the inherited class to the constructor of the base class, how can we do that?...
asked Jan 16, 2021 in Technology by JackTerrance
0 votes
    I have a constructor like as follows: public Agent(){ this.name = "John"; this.id = 9; this. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
0 votes
    I have a constructor like as follows: public Agent(){ this.name = "John"; this.id = 9; this. ... , JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 15, 2022 in Education by JackTerrance
...