What do we mean by Inner Classes in Java ?.
Inner Classes in Java provides a way to place definition of a class within another class. The class containing an inner class is termed as enclosing class.
A simple program to demonstrate working of Inner Classes in Java
Output of the program :
Inner Classes in Java provides a way to place definition of a class within another class. The class containing an inner class is termed as enclosing class.
A simple program to demonstrate working of Inner Classes in Java
package com.hubberspot.examples.inner; public class InnerClassTest { public Customer getCustomer() { return new Customer(); } class Customer { private Integer id; private String firstName; private String lastName; private String email; public Customer(){ } public Integer getId() { return id; } public void setId(Integer id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } } public static void main(String[] args) { InnerClassTest outer = new InnerClassTest(); Customer customer = outer.getCustomer(); customer.setId(1); customer.setFirstName("jonty"); customer.setLastName("magicman"); customer.setEmail("jonty@magicman.com"); System.out.println("FirstName : " + customer.getFirstName()); System.out.println("LastName : " + customer.getLastName()); System.out.println("Email : " + customer.getEmail()); } }
Output of the program :