Explain the Initialization of an object using a sample program segment.
Advertisements
प्रश्न
Explain the Initialization of an object using a sample program segment.
कोड लेखन
स्पष्ट कीजिए
Advertisements
उत्तर
Initialization of an Object is the process of assigning initial values to the data members (instance variables) of an object. In Java, an object can be initialized using a constructor, a method or direct assignment through a reference variable.
A constructor is commonly used to initialize an object because it is automatically called when the object is created.
class Student
{
int rollNo;
String name;
Student(int r, String n)
{
rollNo = r;
name = n;
}
void display()
{
System.out.println("Roll No: " + rollNo);
System.out.println("Name: " + name);
}
public static void main(String args[])
{
Student s = new Student(101, "Rahul");
s.display();
}
}
shaalaa.com
क्या इस प्रश्न या उत्तर में कोई त्रुटि है?
