मराठी

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
  या प्रश्नात किंवा उत्तरात काही त्रुटी आहे का?
पाठ 4: Objects - EXERCISE [पृष्ठ १०७]

APPEARS IN

धीरज मेहरोत्रा Computer Science [English] Class 12
पाठ 4 Objects
EXERCISE | Q 9. | पृष्ठ १०७
Advertisements
Share
Notifications

Englishहिंदीमराठी


      Forgot password?
Use app×