Write A Program In Java Which Has A Class Student Having Two Instance Variables Enrollmentno And Name. Create 3 Objects Of Student Class In Main Method And Display Student’s Name

0

Write A Program In Java Which Has A Class Student Having Two Instance Variables Enrollmentno And Name. Create 3 Objects Of Student Class In Main Method And Display Student’s Name

Write A Program In Java Which Has A Class Student Having Two Instance Variables Enrollmentno And Name. Create 3 Objects Of Student Class In Main Method And Display Student’s Name

In this program, we will create a java program in which a class student having two instance variables enrollment number and name and then create 3 objects of student class and last display the name of students. 


Here's a program in java that demonstrates the creation of three objects of the student class and displays their names:


Program
public class Student {
    private int enrollmentNo;
    private String name;
   
    public Student(int enrollmentNo, String name) {
        this.enrollmentNo = enrollmentNo;
        this.name = name;
    }
   
    public String getName() {
        return name;
    }
   
    public static void main(String[] args) {
        Student s1 = new Student(1, "ABC");
        Student s2 = new Student(2, "PQR");
        Student s3 = new Student(3, "XYZ");
       
        System.out.println(s1.getName());
        System.out.println(s2.getName());
        System.out.println(s3.getName());
    }
}


Output
ABC PQR XYZ

Explanation
The program defines a student class with two instance variables, enrollmentno and name, and a constructor that initializes these variables. It also defines a getname method that returns the student's name.

In the main method, it creates three student objects s1, s2, and s3, and initializes them with enrollment numbers and names. Then it calls the getname method on each object and prints the result to the console. This displays the names of all three students.

❤️ I Hope This Helps You Understand, Click Here To Do More Exercise In Java Programming.

Post a Comment

0Comments
Post a Comment (0)