Free Data Structures and Algorithms Course

Subscribe below and get all best seller courses for free !!!


Subscribe to all free courses

A simple program to demonstrate working of Inner Classes in Java

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

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) { = 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) { = email;

   public static void main(String[] args) {

 InnerClassTest outer = new InnerClassTest();  
 Customer customer = outer.getCustomer();


 System.out.println("FirstName : " + customer.getFirstName());
 System.out.println("LastName : " + customer.getLastName());
 System.out.println("Email : " + customer.getEmail());



Output of the program :

© 2021 Learn Java by Examples Template by Hubberspot