Monday, 4 November 2013

Concept of Class and Object simple Example: 


Requirement:
 Write a program to create ‘Student’ class with rollNo and name as two instance variables.
Create two objects student 1, student 2 for the Student class and assign any appropriate values to the objects. Display the Object content on a Console.


Program:
//Student.java
class StudentDetails
{
   int Rollnumber;
   String Name;
   public StudentDetails(int rno,String sname)
{
 Rollnumber=rno;
 Name=sname;
}
public void PrintDetails()
{
System.out.println("..............................\n");
System.out.println("The Student Rollno is:"+Rollnumber);
        System.out.println("The Student Name is:"+ Name);
System.out.println("..............................\n");
}

};//class
public class Student
{


public static void main(String[] args) 
{

  StudentDetails student1=new StudentDetails(1,"Ajay");
  StudentDetails student2=new StudentDetails(2,"Kumar");
  student1.PrintDetails();
  student2.PrintDetails();
}//main

}//class


Output:

..............................

The Student Rollno is:1
The Student Name is:Ajay
..............................

..............................

The Student Rollno is:2
The Student Name is:Kumar
..............................

No comments: