Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Java when is the final block in a constructor executed?

For example:

/**
 * Constructor
 */
public Test(InputStream in){
   try{
      this.inputStream = in;
   } finally{
      inputStream.close();
   }
}

Is the InputStream that is passed to the instructor immediately closed after the Test object is created? Or is it closed when the Test object is destroyed? I don't know how to implement something similar to a destructor in C++.

like image 385
nomnom Avatar asked Oct 31 '13 07:10

nomnom


People also ask

When finally block is executed?

The finally block always executes when the try block exits. This ensures that the finally block is executed even if an unexpected exception occurs.

In what order is the constructor executed?

Constructors of Virtual base classes are executed, in the order that they appear in the base list. Constructors of nonvirtual base classes are executed, in the declaration order. Constructors of class members are executed in the declaration order (regardless of their order in the initialization list).

Which block executed just before constructor in Java?

In a Java program, operations can be performed on methods, constructors and initialization blocks. Instance Initialization Blocks : IIB are used to initialize instance variables. IIBs are executed before constructors. They run each time when object of the class is created.

Is finally block always executed?

A finally block always executes, regardless of whether an exception is thrown.


2 Answers

It's executed as part of a constructor. The code executed within a constructor is just normal code - there's no odd control flow there. (I mean after the call to the superclass constructor, and the variable/instance initializers being run.)

There's no equivalent to a C++ destructor in Java. The closest you'll get is a finalizer, but that should not be used as the equivalent of a C++ destructor. (You should hardly ever write finalizers. There are cases where they're not called on shutdown, and they're called non-deterministically.)

In the case you've given, you would probably not want your class to assume responsibility for closing the input stream - usually the code which opens the stream is responsible for closing it as well. However, if you did want to take responsibility for that, or just make it easier for callers, you would probably want to expose a close() method which just closes the stream. You might want to implement AutoCloseable too, so that callers can use a try-with-resources statement with your class:

try (Test test = new Test(new FileInputStream("foo.txt")) {
    // Do stuff with test
}

That will call test.close() automatically at the end of the try block.

like image 53
Jon Skeet Avatar answered Sep 28 '22 08:09

Jon Skeet


Finally will be immediately executed after try. It does not matter it is in constructor. It is well defined in JLS.

It says:

A try statement with a finally block is executed by first executing the try block. Then there is a choice:

  • If execution of the try block completes normally, then the finally block is executed, and then there is a choice:
    • If the finally block completes normally, then the try statement
      completes normally.
    • If the finally block completes abruptly for reason S, then the try statement completes abruptly for reason S.
like image 30
Subhrajyoti Majumder Avatar answered Sep 28 '22 08:09

Subhrajyoti Majumder