Posts

Showing posts with the label java9

Java 9 - Try with resources Improvement

Java 9   -   Try with resources Improvement In this blog, I’ll discuss about try with resources which was introduced with JDK 7 and what enhancement has been done in JDK 9. Try with resources is a great feature which was introduced in JDK 7 that helps in closing resources automatically after being used. Any class can be used as resources if that class implements AutoClosable Interface. Advantages As try with resources closes all the resources file Automatically which prevents memory leaks. M ore readable code as you don’t have to write unnecessary code.   Let’s understand it with an example import java.io.FileNotFoundException; import java.io.FileOutputStream; /**  * This class will explain the try with resources which was introduced with  * JDK7.  *  * @author AbdulWaheed18 @gmail.com  *  */ public class ExmapleWithJava7 {        public static void...

Private method in Java 9

Image
As we know till Java 7, we are not allowed to add any concrete function to the Interface, All the function should be abstract and must be implemented in Child class which is implementing the interface. i.e. an interface can only have Constant variable abstract method With Java 8, we can add static and default method as well in an Interface. Check my blog on Java 8 for more details. So, an Interface now can have Constant Variable Abstract Method Default Method Static Method and with Java 9, It become more powerful and now we can add private method and private static method. but why do we need private function in an Interface. Let’s understand this with an example. In above example, we can observe that all the default function has same code to create database connection (duplicate code) and fetching the data and database details is also exposed to outside the world. So over here, Private method will come to rescue. Che...