Pin It

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) {
  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 :




Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Related Posts Plugin for WordPress, Blogger...

Post your related queries below ...

comments powered by Disqus
Powered by Blogger.
 
© 2012 Learn Java by Examples Template by Hubber Spot