final - final keyword can be used with a class, variable or a method.
The underlying behavior of using final keyword is to act as constant.
public class Test {
private static final String PREFIX = "test."
private final MyClass obj = new Myclass();
publc Test() {
obj = new MyClass() ;// throws error
}
}
public class Test {
private static final String PREFIX = "test."
private final MyClass obj;
publc Test() {
obj = new MyClass() ;// this works
}
}
public class Test {
public static void main(String[] args) {
BufferedReader br = null;
try {
String sCurrentLine = "";
br = new BufferedReader(new FileReader("C:\\testing.txt"));
while ((sCurrentLine = br.readLine()) != null) {
System.out.println(sCurrentLine);
}
} catch (IOException e) {
e.printStackTrace();
} finally { // close the resource.
try {
if (br != null)br.close();
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
finalize() - This is the method of Object class.It is invoked before an object is discarded by the garbage collector, allowing it to clean up its state. Should not be used to release non-memory resources like file handles, sockets, database connections etc because Java has only a finite number of these resources and you do not know when the garbage collection is going to kick in to release these non-memory resources through the finalize() method.