w3resource

Java Static Nested Class with Non-static Methods Example

Java Nested Classes: Exercise-10 with Solution

Static Nested Class and Non-static Methods:
Write a Java program to create an outer class called School with a static nested class Student. The Student class should have a non-static method displayStudentInfo() that prints the student's name and grade. Instantiate the Student class from the main method and call the displayStudentInfo() method.

Sample Solution:

Java Code:

School.java

// Outer class School
public class School {
    // Static nested class Student
    static class Student {
        // Instance variables for student's name and grade
        String name;
        String grade;

        // Constructor for Student class
        Student(String name, String grade) {
            this.name = name;
            this.grade = grade;
        }

        // Non-static method to display student information
        void displayStudentInfo() {
            System.out.println("Student Name: " + name);
            System.out.println("Grade: " + grade);
        }
    }

    // Main method to demonstrate creating an instance of the static nested class
    public static void main(String[] args) {
        // Creating an instance of the static nested class Student
        School.Student student = new School.Student("Julius Helga", "A");

        // Calling the displayStudentInfo method to print student details
        student.displayStudentInfo();
    }
}

Output:

Student Name: Julius Helga
Grade: A

Explanation:

  • Outer class School: The main class that contains the static nested class.
  • Static nested class Student: Defined inside the School class, with a constructor and a non-static method.
  • Instance variables name and grade: To hold the student's name and grade values.
  • Constructor Student(String name, String grade): Initializes the name and grade variables.
  • Method displayStudentInfo: Prints the student's name and grade.
  • Main method: Demonstrates creating an instance of the static nested class Student from the School class and printing the student details.
  • Creating an instance of Student: Directly using the School class to create a new Student.
  • Calling displayStudentInfo method: To print the student's name and grade details.

Note on Java Nested Classes:

Java nested classes, including static nested classes, allow for a logical grouping of classes that are only used in one place, enhancing code readability and maintainability. In this exercise, the static nested class Student within the outer class School demonstrates how a static nested class can have non-static methods and instance variables. This enables creating objects that are logically associated with the outer class while providing flexibility in method definitions and usage.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java Static Members Previous: Java Inner Class with Constructor Example.

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