Creating Objects and use of this keyword


Objects are an instance of the blueprint of a defined element in java language known as class. Objects defines how you denote this class as a real component. "this" keyword will help you to take care of objects. to know how read more.

Creating Objects of Class

Creating object in Java is simple as declaring the variable. I would rather describe this with two part entity. one is the object itself and other is the object reference variable which holds the reference to the object. Just like any other variable the reference variable acts as a container of the address for the actual memory location.
We create an Object by calling the Constructor of that class.

Car a = new Car(); // creating object using default constructor
or if the Class contains the parameterized constructor then you can do it like this too.
Car a = new Car(10); //creating object using parameterised constructor

When you call a constructor of any class this creates the object in memory and returns a reference to this object. You can either use this in the code directly or can store it in some reference variable of that class type for which it is made. There are a few more concepts with the type of reference in which you can store an object. For example you can store an object in the Reference of type "Object" which is parent class of all the classes in Java.

Object o = new SomeClass(); //create object and store in the Object reference

or you can store the object in the reference of its parent class, that is a concept of Inheritance.
For example consider we have two classes A and B. A is a parent class and B is sub class. and we create an Object of type B we can store it in A's reference.

A a = new B(); //storing the object of subclass in superclass reference type

Assigning Object Reference Variable you need to remember that whenever you use an Object as the argument in any method or constructor the passed value is always its reference not the object itself for example consider this constructor which takes the Object as an argument.


Class A{
    B b; //where b is another class
    A(B b){
        this.b = b; //passing the reference got at runtime to the A’s b variable.
}
public static void main(String...args){
    B b = new B(); //create object of B class
    A a = new A(b); //pass the B’s object reference to A’s constructor.
}
}

The “this” keyword

the this keyword is used to refer the current object. This can also be used to refer another constructor from inside a constructor. But usually you’ll find this where there is shadowing with the variables. taking the example of the car


class Car{
    int speed;
    boolean transmissionMode;
    Car(){
this(10,true); //referring the parameterized constructor from the default constructor
}
//parameterized constructor doing shadowing for speed and transmission mode
Car(int speed, boolean transmissionMode){
    this.speed = speed;
    this.transmissionMode = transmissionMode;
}
}

“this” in this case will refer to the current object for which the constructor will be called.

NOTE: The this keyword in java in meaning works as the C++ or javascript this but in real there is lot of difference.