15 November 2016

Selected Java Interview Questions Part – 1

Constructor is just like a method that is used to initialize the state of an object. It is invoked at the time of object creation.
eg:
class Student4{
int id;
String name;
Student4(int i,String n){
id = i;
name = n;
}
void display(){System.out.println(id+” “+name);}
public static void main(String args[]){
Student4 s1 = new Student4(111,”Karan”);
Student4 s2 = new Student4(222,”Aryan”);
s1.display();
s2.display();
}
}
Output:
111 Karan
222 Aryan

<<<Click here for more >>>>


No comments:

Post a Comment