सीधे मुख्य सामग्री पर जाएं

#41 What is Constructor in Java ? How to create Constructor ? Full Explanation about Constructor

package com.sudhirtheindian2.java123;

public class MyConstructor {

int x;
String name;


MyConstructor(){
x=0;
name=null;

}

void display(){
System.out.println(x+" "+name);
}

public static void main(String[] args) {

MyConstructor c = new MyConstructor();
c.display();

// What is constructor in java ?

// constructor is a special type of method
// whose name is same as class name

// important notes for constructor--------

// 1 the main purpose of constructor is initialize the object

// 2 every java class has a constructor
// if you not create constructor then
// java create autumatic default constructor

// 3 constructor is autmatically called
// at the time of object creation

// 4 constructor never contain any return types


// syntax
// constructor name as a class name




}
}

टिप्पणियाँ

इस ब्लॉग से लोकप्रिय पोस्ट