What is static nested class and usage in Java

Question -What is static nested class and usage in Java

Solutions– Many developers are confused and telling static nested class as static inner class.But in java, there is no static inner class. 😮

Ok.First we understand ,

what is inner class – inner class or nested class is a class declared entirely within the body of another class or interface.An instance of a normal or top-level class can exist on its own. By contrast, an instance of an inner class cannot be instantiated without being bound to a top-level class.

class OuterClass {

  class InnerClass {  }

}

Types of nested classes in Java

Member class – They are declared outside a function (hence a “member”) and not declared “static”.
Local class
Anonymous class

Nested or inner class is a member of its enclosing class.
Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class.A static nested class interacts with the instance members of its outer class (and other classes) just like any other top-level class. In effect, a static nested class is behaviorally a top-level class that has been nested in another top-level class for packaging convenience.

Why we use– if a class does’nt depend on anything other than his outer class then we will create static nested class.for example – we have nThere is no need for LinkedList.Entry to be top-level class as it is only used by LinkedList (there are some other interfaces that also have static nested classes named Entry, such as Map.Entry – same concept). And since it does not need access to LinkedList’s members, it makes sense for it to be static – it’s a much cleaner approach.

Happy learning with Vinay in techartifact…

Update or edit existing Excel files in Java using Apache POI

Requirement -Programatically update an Excel spreadsheet in Java.

Solutions – Here an example of Apache POI to update a XLS workbook. Apache POI is a very good API , that can be handy in manipulating Excel documents.This tutorial focuses on XLS documents (Office 97 – 2003).

The existing excel file like below –

sdsdsdsdsdsdsd

Below is code to update the excel file

import java.io.*;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.ss.usermodel.*;
import java.util.Iterator;
public class updateExcels {  

        public static void main(String[] args) throws Exception{
                
                FileInputStream fsIP= new FileInputStream(new File("C:\\TechartifactExcel.xls")); //Read the spreadsheet that needs to be updated
                 
                HSSFWorkbook wb = new HSSFWorkbook(fsIP); //Access the workbook
                 
                HSSFSheet worksheet = wb.getSheetAt(0); //Access the worksheet, so that we can update / modify it.
                 
                Cell cell = null; // declare a Cell object
               
                cell = worksheet.getRow(2).getCell(2);   // Access the second cell in second row to update the value
                 
                cell.setCellValue("OverRide Last Name");  // Get current cell value value and overwrite the value
                 
                fsIP.close(); //Close the InputStream
                
                FileOutputStream output_file =new FileOutputStream(new File("C:\\TechartifactExcel.xls"));  //Open FileOutputStream to write updates
                 
                wb.write(output_file); //write changes
                 
                output_file.close();  //close the stream    
        }
}

after updating excel file would look like below –

1updateexcel2

happy coding with Vinay in techartifact ….

Shallow copy and deep copy in prototype patterns?

There are two types of cloning for prototype patterns. One is the shallow cloning which you have just read in the first question. In shallow copy only that object is cloned, any objects containing in that object is not cloned. For instance consider the figure ‘Deep cloning in action’ we have a customer class and we have an address class aggregated inside the customer class. ‘MemberWiseClone’ will only clone the customer class ‘ClsCustomer’ but not the ‘ClsAddress’ class. So we added the ‘MemberWiseClone’ function in the address class also. Now when we call the ‘getClone’ function we call the parent cloning function and also the child cloning function, which leads to cloning of the complete object. When the parent objects are cloned with their containing objects it’s called as deep cloning and when only the parent is clones its termed as shallow cloning.