Class and Object Example: Initialization through method

Java Programming Language Class, Object and Methods in java (Article) Class, Object and Methods in java (Program)

1539

Program:

// Save it as File name : StudentClassExample4.java
//Class and object Example: Initialization through method

class Student{
 int rollno;
 String name;
 void insertRecord(int r, String n)
   {
  rollno=r;
  name=n;
  }

 void displayInformation()
      {
	   System.out.println(rollno+" "+name);
	  }
}


class StudentClassExample4{
 public static void main(String args[]){

  Student s1=new Student();
  Student s2=new Student();
  s1.insertRecord(111,"Rabindranath");
  s2.insertRecord(222,"Alibaba");
  s1.displayInformation();
  s2.displayInformation();

 }
 }

Output:

111 Rabindranath
222 Alibaba
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.