Java Local Class Accessing Local Variables Example
Java Nested Classes: Exercise-7 with Solution
Local Class Accessing Local Variables:
Write a Java program to create a class called House with a method calculateArea(). Inside this method, define a local class Room with a method getArea() that calculates and returns the area of the room (length * width). Instantiate the Room class and call the getArea() method from within calculateArea().
Sample Solution:
Java Code:
House.java
// Class House
public class House {
// Method calculateArea to calculate the area of a room
public void calculateArea() {
// Local variables for length and width of the room
final double length = 12.5;
final double width = 7.0;
// Local class Room inside the calculateArea method
class Room {
// Method getArea to calculate and return the area of the room
public double getArea() {
return length * width;
}
}
// Instantiating the local class Room
Room room = new Room();
// Calling the getArea method and printing the area
double area = room.getArea();
System.out.println("Area of the room: " + area);
}
// Main method to demonstrate the usage of local class
public static void main(String[] args) {
// Creating an instance of House and calling calculateArea method
House house = new House();
house.calculateArea();
}
}
Output:
Area of the room: 87.5
Explanation:
- Class House: The outer class containing the method calculateArea().
- Method calculateArea(): Defines local variables length and width and contains a local class Room.
- Local class Room: Defined inside calculateArea(), with a method getArea() that calculates and returns the area of the room.
- Method getArea(): Uses the local variables length and width to calculate the area.
- Instantiation and usage: Inside calculateArea(), an instance of Room is created and its getArea() method is called to print the area of the room.
- Main method: Demonstrates the usage of the House class and its calculateArea() method by creating an instance of House and calling calculateArea().
Note on Java Nested Classes:
Java nested classes can be local, meaning they are defined within a method. In this exercise, the Room class is defined inside the "calculateArea()" method of the House class. The local class Room can access the final local variables length and width defined in the enclosing method. This demonstrates how local classes can be used to encapsulate functionality that is only relevant within a specific method, promoting better organization and readability of code.
Java Code Editor:
Improve this sample solution and post your code through Disqus.
Java Static Members Previous: Java Static Nested Class and Static Methods Example.
Java Static Members Next: Java Anonymous Class Implementing Abstract Class 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-7.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics