w3resource

Java Program for Static Nested Class - University and Department

Java Nested Classes: Exercise-2 with Solution

Static Nested Class:
Write a Java program to create an outer class called University with a static nested class Department. The Department class should have a method "displayInfo()" that prints the department name and the number of faculty members. Instantiate the Department class from the main method and call the "displayInfo()" method.

Sample Solution:

Java Code:

University.java

// Outer class University
public class University {
    
    // Static nested class Department
    static class Department {
        // Instance variables for Department
        private String departmentName;
        private int facultyCount;
        
        // Constructor to initialize Department
        public Department(String departmentName, int facultyCount) {
            this.departmentName = departmentName;
            this.facultyCount = facultyCount;
        }
        
        // Method to display department information
        public void displayInfo() {
            System.out.println("Department: " + departmentName);
            System.out.println("Number of Faculty Members: " + facultyCount);
        }
    }
    
    // Main method to demonstrate the static nested class
    public static void main(String[] args) {
        // Creating an instance of the static nested class Department
        University.Department compSciDept = new University.Department("Computer Science", 50);
        
        // Calling the displayInfo method
        compSciDept.displayInfo();
    }
} 

Output:

Department: Computer Science
Number of Faculty Members: 50

Explanation:

  • Outer class University: The outer class that contains the static nested class.
  • Static nested class Department: This class is nested within the University class and is declared as static.
  • Instance variables for Department: departmentName and facultyCount are used to store the name of the department and the number of faculty members.
  • Constructor for Department: Initializes the departmentName and facultyCount variables.
  • Method displayInfo(): Prints the department name and the number of faculty members.
  • Main method: Demonstrates the usage of the static nested class by creating an instance of Department and calling the displayInfo() method.

Note on Java Nested Classes:

Here Java nested classes allow you to logically group classes that are only used in one place. This can increase the use of encapsulation and create more readable and maintainable code. In this exercise, the Department class is a static nested class within the University class, allowing it to be associated with the University class without needing an instance of University. This is useful for grouping related classes together.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Static Members Previous: Java program to demonstrate Inner Class Functionality.
Java Static Members Next: Java Program with Local Class in Method - Car and Engine.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

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-2.php