Lab3: Object Creation in Java

Lab3: Object Creation in Java

LAB OBJECTIVE: Full understanding about object creation and its reference variable, reference ID, and object buffer area.

Object: is nothing but it is just a memory or a buffer in a heap area, in which all the instance data members are getting memory. An object is just buffer, it can create anywhere but it will get memory always into heap area only.

Let’s consider an example of how an object created and works with instance function. For example, consider an object of the employee.

//save as: Employee1.java
class Employee1{
        //Data members
	String name;
	int salary;
	String cName;
	void get(String s1, int s2, String s3){
		name=s1;
		salary=s2;
        cName=s3;
	}
	void show(){ 
		System.out.println(name);
		System.out.println(salary);
		System.out.println(cName);
	}
	public static void main(String args[]){
		Employee1 emp=new Employee1(); 								 
		emp.get("LALU",501,"Google");   
		emp.show();
	
		Employee1 emp1=new Employee1();
		emp1.get("pappu",201,"Congress");
		emp1.show();
		
		System.out.println("reference id of emp:  "+emp);
		System.out.println("reference id of emp1  :"+emp1);
	}
}

Output

F:\coreJAVA\3Objects>javac Employee1.java

F:\coreJAVA\3Objects>java Employee1
LALU
501
Google
pappu
201
Congress
reference id of emp:  Employee1@15db9742
reference id of emp1  :Employee1@6d06d69c

Use of ‘static’ keyword

Always keep those behaviors of an object as static which is not change throughout the program

//save as: Employee.java
class Employee{
        //Data members
	String name;
	int salary;
	static String Cname="TCS"; // static thing is properties of class
	
	void get(String s1, int s2){
		name=s1;
		salary=s2;
	}
	void show(){ 
		System.out.println(name);
		System.out.println(salary);
		System.out.println(Cname);
	}
	public static void main(String args[]){
		Employee emp=new Employee(); 								 
		emp.get("LALU",501);   
		emp.show();
	
		Employee emp1=new Employee();
		emp1.get("pappu",201);
		emp1.show();
		
		System.out.println("reference id of emp:  "+emp);
		System.out.println("reference id of emp1  :"+emp1);
	}
}
//note: always keep those behaviors of an object as static which is not change throughout the program

Output

F:\coreJAVA\3Objects>javac Employee.java

F:\coreJAVA\3Objects>java Employee
LALU
501
TCS
pappu
201
TCS
reference id of emp:  Employee@15db9742
reference id of emp1  :Employee@6d06d69c

The object is either reachable or unreachable, reference id is the address of the memory location of java object which is contained into the reference variable. We can say the reference variable always contains reference id of java object. reference id is nothing but a combination of a class name followed by spacial character followed by hexadecimal numbers: className@<HexaDecimalNumber>.

We can keep the Reference Id of one object into another reference variable

//save as: R_ID.java
class Employee3{
	String name;
	public static void main(String args[]){
		//creation of object of Employee
		Employee3 emp=new Employee3();
		emp.name="LALU";
		// kepping the reference id into another reference variable
		Employee3 emp1=emp;  
		//System.out.println(emp1);
		System.out.println(emp1.name);
		System.out.println(emp);
		if(emp==emp1){
			System.out.println("equals");
		}
		emp1.name="Bhalu";
		System.out.println(emp.name);
	}
}

Output

F:\coreJAVA\3Objects>javac R_ID.java

F:\coreJAVA\3Objects>java Employee3
LALU
Employee3@15db9742
equals
Bhalu