简体   繁体   中英

call a method of inner class from another class Java

I would like to call the Salarycal() method from Employee inner class employeeInfo to class Exc

public class Employee {

    public class employeeInfo{
            int id;
            String name;
            int Salary;


            public  employeeInfo(int id,String name,int Salary){
                this.id=id;
                this.name=name;
                this.Salary=Salary;

                System.out.println(id+name+Salary);
            }   
            public int Salarycal(){
                int totalSalary =0;
                int b=getId();
                           ........
            }
}
import Employer.Employee.employeeInfo;

public class Exc {


    public static void main(String[] args) {
         //want to access the salarycal() method in this class
        }

}

this is how I would usually invoke a method of inner class

Inner inner = new Outer().new Inner();
inner.methodToInvoke();

Like @Ashish said:

public static void main(String[] args) {
    new Employee().new employeeInfo().Salarycal();
}

PS. Read about Code Conventions for the Java TM Programming Language

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM