Class and Object Example: Initialization through constructor

Java Programming Language / Class, Object and Methods in java

1248

Program:

 // Save it as File name : StudentClassExample5.java
// Class and Object Example: Initialization through constructor
class Student{
    int rollno;
    String name;
    float age;
    void insert(int i, String n, float s) {
        rollno=i;
        name=n;
        age=s;
    }
    void display()
    {
		System.out.println(rollno+" "+name+" "+age);
	}
}

public class StudentClassExample5 {
public static void main(String[] args) {
    Student e1=new Student();
    Student e2=new Student();
    Student e3=new Student();
    e1.insert(101,"Ram",20.0f);
    e2.insert(102,"Rahim",20.1f);
    e3.insert(103,"Jack",20.4f);
    e1.display();
    e2.display();
    e3.display();
}
}

Output:

101 Ram 20.0
102 Rahim 20.1
103 Jack 20.4
Press any key to continue . . .

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.