Java Inner Class with Constructor Example
Java Nested Classes: Exercise-9 with Solution
Inner Class with Constructor:
Write a Java program to create an outer class called Person with an inner class Address. The Address class should have a constructor that takes parameters city and state. Create an instance of Address from the Person class and print the address details.
Sample Solution:
Java Code:
Person.java
// Outer class Person
public class Person {
// Inner class Address
class Address {
// Instance variables for city and state
String city;
String state;
// Constructor for Address class
Address(String city, String state) {
this.city = city;
this.state = state;
}
// Method to display address details
void displayAddress() {
System.out.println("City: " + city + ", State: " + state);
}
}
// Main method to demonstrate creating an instance of the inner class
public static void main(String[] args) {
// Creating an instance of the outer class Person
Person person = new Person();
// Creating an instance of the inner class Address
Person.Address address = person.new Address("New Delhi", "ND");
// Calling the displayAddress method to print address details
address.displayAddress();
}
}
Output:
City: New Delhi, State: ND
Explanation:
- Outer class Person: The main class that contains the inner class.
- Inner class Address: Defined inside the Person class, with a constructor and a method to display address details.
- Instance variables city and state: To hold the city and state values.
- Constructor Address(String city, String state): Initializes the city and state variables.
- Method displayAddress: Prints the city and state details.
- Main method: Demonstrates creating an instance of the inner class Address from the Person class and printing the address details.
- Creating an instance of Person: Required to create an instance of the inner class.
- Creating an instance of Address: Using the instance of Person to create a new Address.
- Calling displayAddress method: To print the address details.
Note on Java Nested Classes:
Java nested classes, including inner classes, allow for better organization and encapsulation of code. In this exercise, the inner class Address within the outer class “Person” demonstrates how an inner class can have its own constructor and methods. This enables the creation of objects that are closely associated with their outer class, providing a way to logically group classes that are only used in one place and enhancing code readability and maintainability.
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java Static Members Previous: Java Anonymous Class Implementing Abstract Class Example.
Java Static Members Next: Java Static Nested Class with Non-static Methods Example.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/nested-classes/java-nested-classes-exercise-9.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics