Procedure in Creating Object.
0Procedure in Creating Object:
In an object creation there are two phases
1) Object memory creation.
2) Object memory initialization.
The above two phases has four stemps.
example: Student s1=new Student
step-1: Referenced variable memory creation.
step-2: New keyword execution.
2.1: As part of this step object memory is created with an non-static variables by assigning default values and then assigned value.
step-3: Invoking constrantes
new keyword invokes constracts by passing current object reference.
3.1: As part of this step control is send to constrants body and reference is store by creating this keyword memory forther constraints logic is executed for initializing this object non-static variables.
step-4: After control return from constracter, new keyword return current object reference and store the reference is 's1' variable then 's1' variable start pointing to this object.
// Prog: NetloJavaInstitute.java
//Student.java
class Student {
int rollNo;
String name;
String institute;
Student(int rn,String nm,String ins){
rollNo=rn;
name=nm;
institute=ins;
}
}
//NetloJavaInstitute.java
class NetloJavaInstitute{
public static void main(String args[]){
Student s1=new Student(1,"james","netlojava");
Student s2=new Student(2,"gosling","netlojava");
System.out.println("Student 1 Object Data");
System.out.println("Roll No:"+s1.rollNo);
System.out.println("Name:"+s1.name);
System.out.println("Institute:"+s1.institute);
System.out.println("\nStudent 2 Object Data");
System.out.println("Roll NO:"+s2.rollNo);
System.out.println("Name:"+s2.name);
System.out.println("Institute:"+s2.institute);
}
}
compilation:
G:\netlojava>javac NetloJavaInstitute.java
Execution:
G:\netlojava>java NetloJavaInstitute
Out Put:
Student 1 Object Data
Roll No:1
Name:james
Institute:netlojava
Student 2 Object Data
Roll NO:2
Name:gosling
Institute:netlojava
0 comments: