Create Multiple Objects
This program creates multiple objects of a class.
Problem Statement
Write a Java program to create multiple Student objects and print their details.
Source Code
| 1 | class Student { |
| 2 | String name; |
| 3 | int age; |
| 4 | } |
| 5 | public class MultipleObjects { |
| 6 | public static void main(String[] args) { |
| 7 | System.out.println("Eduinq Multiple Objects"); |
| 8 | Student s1=new Student(); |
| 9 | s1.name="Priya"; s1.age=21; |
| 10 | Student s2=new Student(); |
| 11 | s2.name="Rahul"; s2.age=22; |
| 12 | System.out.println("Student1: "+s1.name+", Age: "+s1.age); |
| 13 | System.out.println("Student2: "+s2.name+", Age: "+s2.age); |
| 14 | } |
| 15 | } |
Program Output
Eduinq Multiple Objects Student1: Priya, Age: 21 Student2: Rahul, Age: 22
Explanation
Multiple objects created from same class. - Each object holds independent data. - Together demonstrates object creation. Conclusion: Shows how to create multiple objects of a class.