Parameterized Constructor
This program uses parameterized constructor to initialize object.
Problem Statement
Write a Java program to initialize Student object using parameterized constructor.
Source Code
| 1 | class Student { |
| 2 | String name; |
| 3 | int age; |
| 4 | Student(String n,int a){ |
| 5 | name=n; age=a; |
| 6 | } |
| 7 | } |
| 8 | public class ParameterizedConstructor { |
| 9 | public static void main(String[] args) { |
| 10 | System.out.println("Eduinq Parameterized Constructor"); |
| 11 | Student s=new Student("Arun",20); |
| 12 | System.out.println("Name: "+s.name+", Age: "+s.age); |
| 13 | } |
| 14 | } |
Program Output
Eduinq Parameterized Constructor Name: Arun, Age: 20
Explanation
Constructor initializes attributes directly. - Object created with values passed. - Together demonstrates parameterized constructor. Conclusion: Shows how to use parameterized constructor.