In Java everything is an Object. Hereby lets start with the basic concepts of Object Oriented Programming a.k.a. OOPs. Class can be viewed as an Entity whereas Object can be defined as attributes and behavior under a single entity. Object is defined as an instance of a Class.
Now we will understand what is an object in Java, how to create objects of a class. Since by this time you are doing well with setting your class-path properly to run Java programs, let’s start with the code.
Java Program
class Employee{ String employeeName; String address; int age; double salary; void showDetails(){ System.out.println("Employee's Name: "+employeeName); System.out.println("Employee's Address: "+address); System.out.println("Employee's Age: "+age); System.out.println("Employee's Salary: "+salary); } } class EmployeeDemo{ public static void main(String args[]){ Employee e; e = new Employee(); System.out.println("Memory address of e: "+e); Employee employee = new Employee(); System.out.println("Memory address of employee: "+employee); Employee emp; emp = employee; System.out.println("Memory address of emp: "+emp); employee = null; System.out.println("Memory address of emp: "+employee); } }
Key Concepts
First look at the java program name we have saved in file with .java extension. It’s not with Employee class rather the java file name is in the name of EmployeeDemo class. The reason is main() method is there within EmployeeDemo class.
When we compile EmployeeDemo.java, we will get two .class file viz. Employee.class and EmployeeDemo.class. As java compiler automatically puts each class into its own .class file, they may not remain in same source file.
Output
Explanation of the Java Code & Outputs
When you create a class, you are creating a new data type. Obtaining objects is a two step process:
First you must declare a variable of the class type. This variable is called reference variable of class Employee. It is simply a variable that can refer to an object.
Secondly, you must acquire an actual, physical copy of the object and assign it to the variable which can be done using new operator.
Function of ‘new’ Operator
The new operator dynamically allocates memory for an object and returns a reference (address in memory of the object allocated by new) to it. This reference is then stored in the variable.
Now,
Employee e; –> Here ‘e’ is a reference variable of class Employee that can refer to an object. It contains null value.
Then,
e = new Employee();
Here new has dynamically allocated memory for the object and returned the reference which is stored in e. Immediately upon creation of object, the member variables are initialized by the default values by the default constructor Employee(). We will discuss constructor later. Refer the output “Memory address of e : Employee@19821f”, it’s clear that it’s nothing but the memory address which is allocated to the object and stored in e.
In the 2nd case Employee employee = new Employee(); the two step process is done in single step. Different memory location is allocated to this object. It’s clear from the output.
In the 3rd case we have created reference variable emp of Employee. Then we have assigned employee object to this reference variable. From the output it’s pretty much sure that both the objects are referring to the same memory location. At the next step employee is set to null, then it will no longer have the copy of members of class Employee.
Here, employeeName, address, age, salary are called member variables or instance variables of class Employee and showDetails() is called the member method of class Employee.
Special Note: Each object will have separate copy of the members of the Employee class. We will understand this diagrammatically.
Next we will learn how to set instance variables of a Class.
Checkout more useful tutorials and definitive guidelines on Java programming here.
5 Comments