How can create an object in Java?


We can create an object in java as simple using the syntax.
Syntax:
class-name  ref-variable  =  new  class-name();
Let us suppose that a class student.
class Student {     int sid=110;
 long phone=1110011;
                             void display(){
                                                System.out.println(sid);
                                                System.out.println(phone);                                                                                   }
  }


class Test{
                   public static void main(String as[]){
          Student s1= new Student();
          s1.display();// we can call member or method of class using dot operator
}
}





Now let us see
Student    s1   =    new Student();
1.      Student  S1                S1 is called “reference variable or object variable”.
        


Null
                                    S1                                                                                                                                                                                                                                                                                                                                                                                                                   
2.      new Student()                                                called “object”                      
                                               


0


0


        Sid                  phone               
                                                           



3.      Student() is call 0 argument constructor. We will see later about constructor

0 comments :

Post a Comment