We already discussed about classes and objects earlier and also we created objects by using the new operator in several examples. Once again, we will go to basics and try to understand how the objects are created and stored in memory.
Class Definition
A class definition consists of variables and methods. Each class definition is written in a separate file and the file is named as [classname].java; when compiled it converts into byte code file [classname].class file. A class definition is just a prototype. Class definition can have several variables and methods.
public class Dog {
int age;
int size;
String colorName;
Dog(int age, int size, String colorName) {
this.age = age;
this.size = size;
this.colorName = colorName;
}
public int getAge() {
return this.age;
}
public void bark() {
System.out.println("Bark - woof, woof");
}
public String getColor() {
return this.colorName;
}
}
Constructors
Constructors are the methods with the same name as [classname]. These methods are called only when the new class instance or object is being created. When there is no constructor, java uses its default constructor. Constructors can also be overridden, we have already discussed about overloading methods. Constructors are used to initialize the class variables.
Dog(int age, int size, String colorName) {
this.age = age;
this.size = size;
this.colorName = colorName;
}
Declaration
After creating the class definition and compiling the .java file, the class works as same as data type. We declare the objects of the class like we declare the variables of primitive data types.
int age;
Dog dog1;
new Operator
The new Operator creates the instance / object of the class, allocates the physical memory to the object and initializes the variables using the constructor method. And then the object reference is assigned to the object variable. You can use the declaration and creation of the object in one statement.
dog1 = new Dog(3, 4, "WHITE");
Dog dog2 = new Dog(2, 4, "BLACK");
Memory Allocation
Each object has their own copy of variables in the memory. When the object is created, a new memory reference is allocated to the object and each object has their copy of their own variables. In above example, dog1 and dog2 are two objects and they are two separate memory locations.
dog1
↴
age | size | colorName |
3 | 4 | "WHITE" |
dog2
↴
age | size | colorName |
2 | 4 | "BLACK" |
Assigning an object to other object variable
We know that two object variables are two separate references to the objects. In above example, dog1 and dog2 are referring to two different objects of same data type (class). But when we assign, one object to another object reference, the object reference is passed. After the below statement, both object references are same. dog1 is not referring to original data variables any more.
dog1 = dog2;
dog1
↴
age | size | colorName |
2 | 4 | "BLACK" |
dog2
↴
age | size | colorName |
2 | 4 | "BLACK" |
this keyword
When the method is called, the object reference, on which the method is invoked, passed implicitly as a parameter. The this keyword refers to the current class instance and can be used to access its members. It is mainly used to permit the name of a method parameter or a local variable to be same as the instance variable.
Dog(int age, int size, String colorName) {this.age = age;this.size = size;this.colorName = colorName;}
Memory Deallocation / Garbage Collection
As we saw that the memory is dynamically allocated to the object reference variables with available memory locations. If keep allocating memory to the variables and not releasing the memory locations, the memory will get exhausted. It is possible that new operator fails to create new objects. So, Java uses the garbage collection approach. All the memory locations not used in the program are released and recycled to use to allocate new variables. As soon as the program terminates, all the memory locations are released and ready for further use. Garbage collection runs automatically in the background without intervene the programmer.
No comments:
Post a Comment