Updates
  • Starting New Weekday Batch for Java Full Stack Developer on 24th June 2024 @ 02:00 PM to 04:00 PM
  • Starting New Weekend Batch for Java Full Stack Developer on 06th July 2024 @ 11:00 AM to 02:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 08th July 2024 @ 04:00 PM to 06:00 PM
  • Starting New Weekday Batch for Java Full Stack Developer on 29th July 2024 @ 10:00 AM to 12:00 PM
Join Course

java.lang.AutoCloseable

As we know we have a dedicated block to run the important code such as closing the resources.

            
class Resource1{
	Resource1(){
		System.out.println("Resource-1 is open");
	}
	
	void use(int a, int b) throws ArithmeticException{
		System.out.println(a/b);
		System.out.println("Resource-1 is in use...");
	}
	
	void close(){
		System.out.println("Resource-1 is closed");
	}
}
class Resource2{
	Resource2(){
		System.out.println("Resource-2 is open");
	}

	void use(int a, int b) throws ArithmeticException{
		System.out.println(a/b);
		System.out.println("Resource-2 is in use...");
	}
	
	void close(){
		System.out.println("Resource-2 is closed");
	}
}
class JTC{
	public static void main(String arg[]){
		Resource1 r1 = null;
		Resource2 r2 = null;
		try{
			r1 = new Resource1();
			r2 = new Resource2();
			r1.use(Integer.parseInt(arg[0]),Integer.parseInt(arg[1]));
			r2.use(Integer.parseInt(arg[2]),Integer.parseInt(arg[3]));
		}catch(ArithmeticException e){
			System.out.println(e);
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println(e);
		}
		finally{
			r2.close();
			r1.close(); 
		}
					
	
	}
}
    C:\Users\Rahul\Desktop>java JTC
    Resource-1 is open
    Resource-2 is open
    java.lang.ArrayIndexOutOfBoundsException: 0
    Resource-2 is closed
    Resource-1 is closed
            

In the above example as we can see how finally block is working to close resources.

Note:- resources should be close in LIFO (Last-In-First-Out) sequence.

So as per the use-case first we have created a method createStudent() in Utility class:

There is one alternate way to close the resources is same format which is java.lang.AutoCloseableinterface which is introduced in JDK 1.7.

    C:\Users\Rahul\Desktop>javap java.lang.AutoCloseable
    Compiled from "AutoCloseable.java"
    public interface java.lang.AutoCloseable {
        public abstract void close() throws java.lang.Exception;
    }

java.lang.AutoCloseable interface is a Functional Interface which is containing only one abstract method close() as we can see into the above console.

• If you want to develop your custom resources then write your resource class by implementing the java.lang.AutoCloseable interface.
• In java.lang.AutoCloseable interface there is only one method, close().
• This close() method must be overridden inside your class according to the resource requirement.
• If you are using resource inside try with resource then your resource must implement java.lang.AutoCloseable interface, otherwise it will give the compile-time error.
• If we are using try with resource and it is satisfying all the constraints then there is no use of writing any finally block, you can easily write it with that.

            
class Resource1 implements AutoCloseable{
	Resource1(){
		System.out.println("Resource-1 is open");
	}
	
	void use(int a, int b) throws ArithmeticException{
		System.out.println(a/b);
		System.out.println("Resource-1 is in use...");
	}
	
	public void close(){
		System.out.println("Resource-1 is closed");
	}
}
class Resource2 implements AutoCloseable{
	Resource2(){
		System.out.println("Resource-2 is open");
	}

	void use(int a, int b) throws ArithmeticException{
		System.out.println(a/b);
		System.out.println("Resource-2 is in use...");
	}
	
	public void close(){
		System.out.println("Resource-2 is closed");
	}
}
class JTC{
	public static void main(String arg[]){
		try(Resource1 r1 = new Resource1();Resource2 r2 = new Resource2()){
			r1.use(Integer.parseInt(arg[0]),Integer.parseInt(arg[1]));
			r2.use(Integer.parseInt(arg[2]),Integer.parseInt(arg[3]));
		}catch(ArithmeticException e){
			System.out.println(e);
		}catch(ArrayIndexOutOfBoundsException e){
			System.out.println(e);
		}	
	}
}
    C:\Users\Rahul\Desktop>javac test.java

    C:\Users\Rahul\Desktop>java JTC
    Resource-1 is open
    Resource-2 is open
    Resource-2 is closed
    Resource-1 is closed
    java.lang.ArrayIndexOutOfBoundsException: 0

    C:\Users\Rahul\Desktop>java JTC 10 2 30 2
    Resource-1 is open
    Resource-2 is open
    5
    Resource-1 is in use...
    15
    Resource-2 is in use...
    Resource-2 is closed
    Resource-1 is closed

    C:\Users\Rahul\Desktop>java JTC 10 2 30 0
    Resource-1 is open
    Resource-2 is open
    5
    Resource-1 is in use...
    Resource-2 is closed
    Resource-1 is closed
    java.lang.ArithmeticException: / by zero
            

In the above example, as we can see, we are creating two custom resources, Resource1 and Resource2, by implementing java.lang. AutoCloseable interface and overriding close() method.
And in the JTC class main method, we are creating resources along with the try-with-resource concept, and we can see there is no need to invoke the close() method explicitly by invoking the close() method. This is the main advantage of the AutoCloseable interface and try-with-Resource concept.