Tutoriallearn.com
Easy Learning
Search Tutoriallearn.com :
class student {
int rollno;
String name;
student() // constructor is defined, it has same name as class name
{
rollno = 10;
name = "Rajesh";
}
display ()
{
System.out.println ("roll no is " + rollno);
System.out.println ("name is "+ name);
}
}
class demostudent {
public static void main (String args[])
{
student s1 = new student(); // this statement will call the constructor to
// initialize s1 object.
s1.diaplay();
}
}
roll no is 10 name is Rakesh
class student {
int rollno;
String name;
student(int r, String n) // Parametrized constructor
{
rollno = r;
name = n;
}
display ()
{
System.out.println ("roll no is " + rollno);
System.out.println ("name is "+ name);
}
}
class demostudent {
public static void main (String args[])
{
student s1 = new student(10, "Rajesh"); // this statement will call the constructor to
// initialize s1 object.
s1.diaplay();
}
}
roll no is 10 name is Rakesh