1 /* 2 * Copyright (C) 2011 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package java.lang; 18 19 /** 20 * Defines an interface for classes that can (or need to) be closed once they 21 * are not used any longer. Calling the {@code close} method releases resources 22 * that the object holds. 23 * 24 * <p>A common pattern for using an {@code AutoCloseable} resource: <pre> {@code 25 * Closable foo = new Foo(); 26 * try { 27 * ...; 28 * } finally { 29 * foo.close(); 30 * } 31 * }</pre> 32 * 33 * @since 1.7 34 * @hide 1.7 35 */ 36 public interface AutoCloseable { 37 38 /** 39 * Closes the object and release any system resources it holds. 40 * 41 * <p>Unless the implementing class specifies otherwise, it is an error to 42 * call {@link #close} more than once. 43 */ 44 void close() throws Exception; 45 } 46