You are here:   HomeJava ProgrammingClasses in JavaInner Class Java Example
by Dinesh Thakur Category: Classes

These are non static members classes. Whereas a non static inner classes are defined as instance members of other classes just like instance variable and method members are defined in a class. An instance of non static inner class always has an instance of the enclosing class associated with it.

import java.io.*;

       class SalaryCalculator
         {
            float hra,da,  gross;
            void DisplayHRA(WorkerDetail m)
               {
                   hra=(float)m.salary*60/100;
                   System.out.println("HRA : "+hra);
               }
                   void DisplayDA(WorkerDetail m)
                   {
                        da=(float)m.salary*98/100;
                        System.out.println("DA : "+da);
                   }
                        void DisplayGross(WorkerDetail m)
                          {
                              gross=hra+da+m.salary;
                              System.out.println("Gross Salary : "+gross);
                          }
                              public static void main(String args[])
                                  {
                                      SalaryCalculator s =new SalaryCalculator();
                                      try
                                        {
                                            WorkerDetail k=  s.new WorkerDetail();
                                            k.ShowDetail();
                                            s.DisplayHRA(k);
                                            s.DisplayDA(k);
                                            s.DisplayGross(k);
                                         }
                                            catch(IOException e){}
                                  }
                                        class WorkerDetail
                                          {
                                               int code, salary;
                                               String name;
                                               WorkerDetail() throws IOException
                                                     {
                                                            BufferedReader bf=new BufferedReader(new InputStreamReader (System.in));
                                                            String h;
                                                            System.out.println("Enter Employee Code : ");
                                                            h=bf.readLine();
                                                            code=Integer.parseInt(h);
                                                            System.out.println("Enter Employee Name : ");
                                                            name=bf.readLine();
                                                            System.out.println("Enter Employee Salary : ");
                                                            h=bf.readLine();
                                                            salary=Integer.parseInt(h);
                                                     }
                                                             void ShowDetail()
                                                                  {
                                                                       System.out.println("Employee Code : "+code);
                                                                       System.out.println("Employee Name : "+name);
                                                                       System.out.println("Basic Salary : "+salary);
                                                                  }
                                         }
            }

Inner Class Java Example