How to use inner classes in JAVA?


Here is a example code for testing Inner Classes in Java Environment;

package innerclassexample;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
*
* @author tunatore
*/
public class Department {

private static String deparmentName = “IT Department”;
private static int departmentId = 1;
//Syntax
//OuterClass.StaticInnerClass innerClassObject = new OuterClass.StaticInnerClass();
//location is a static inner class
private Location location = new Department.Location(“Istanbul”);
private List employeesInDepartment = new ArrayList();

//this is a inner class inside Department class
public class Employee {

private int employeeId;
private String name;
private String surname;
//private static String staticVariableIsNotAllowed;
//inner classes can not have static variables error

/**
* @return the employeeId
*/
public int getEmployeeId() {
return employeeId;
}
/**
* @param employeeId the employeeId to set
*/
public void setEmployeeId(int employeeId) {
this.employeeId = employeeId;
}
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the surname
*/
public String getSurname() {
return surname;
}
/**
* @param surname the surname to set
*/
public void setSurname(String surname) {
this.surname = surname;
}
public String getDepartmentInfoInsideEmployeeClass() {

//inner classes can access outer classes variables
String departmentInfo = “Department’s Name–> ” + deparmentName
+ ” Department’s ID –> ” + departmentId
+ “ Department’s Location –> ” + location.getLocationName();
return departmentInfo;
}
}
public static class Location {

private String locationName = “”;
public Location(String name) {
locationName = name;
}
private String getLocationName() {
return locationName;
}

}

//both of these class declarations are allowed
public class TestClass {}
private class TestClass1 {}
protected class TestClass3 {}
class TestClass4 {}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {

Department department = new Department();

//creating an instance of employee inner class by using outer class
//Syntax
//OuterClass.InnerClass innerClassObject = outerClassObject.new InnerClass();
Department.Employee employee = department.new Employee();
employee.employeeId = 1;
employee.name = “Tuna“;
employee.surname = “Tore“;

Department.Employee employee2 = department.new Employee();
employee2.employeeId = 2;
employee2.name = “James“;
employee2.surname = “Gosling“;

System.out.println(employee.getDepartmentInfoInsideEmployeeClass());

department.employeesInDepartment.add(employee);
department.employeesInDepartment.add(employee2);

System.out.println(“Employees in Department –> ” + department.deparmentName);

for (Iterator it = department.employeesInDepartment.iterator(); it.hasNext();) {
Employee employeeObj = (Employee)it.next();
System.out.println(employeeObj.getEmployeeId() + ” ” + employeeObj.getName() + ” ” + employeeObj.getSurname());

}

}

}

Output of the program

run:
Department’s Name–> IT Department Department’s ID –> 1 Department’s Location –> Istanbul
Employees in Department –> IT Department
1 Tuna Tore
2 James Gosling
BUILD SUCCESSFUL (total time: 1 second)

Download Source

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s