Skip to main content

Class , Constructors and this keyword in Java




#Topic-8

Previous topic (7) we have discussed about How to convert String into primitive data types and primitive data types into String. If you have not gone through that topic, click this link How to convert String into primitive data types and primitive data types into String.
 
In this section we will discuss about Class , Constructors and this keyword in Java.

Class: Class is a container we can store ‘n’ no of information or details within a class. That details/information must be specified in class name.
Class is a collection of Data and Methods, that data and methods we can accessed based on class object or instance.

1)  Constructors in Java
Constructor is a special type of method that is used to initialize the object.java constructor is invoked at the time of object creation.
It construct the values (i.e. provides data for the object) that is why it is known as constructor.

· Rules:
i)    Constructor name must be same as its class name.
ii)    Constructor must have no explicit return type.

·   2 types of constructor:
1.     Default constructor
2.     Parameterized constructor

1. Default constructor: it provides null or zero default values (depending on type) to the object.

2. Parameterized constructor: it is used to provide default values to the object.

Note: if there is no constructor in a class, compiler automatically creates a default constructor.  

Note: If class contain only parameterized constructor, then we cannot create default constructor class object in main class (occur compile time error) so we need to define default constructor in in a class. Without constructor then we need to write code everything in main method, every time we need to initialize values. Constructor avoids lengthy code.

2)   Constructor Overloading
Constructor Overloading is a technique in java which class can have any number of constructor that must differs in parameter data types, sequence data type of each parameter and number of parameter.


3)   this keyword
this is a keyword and reference variable that refers to the current object.

 Usage of java this keyword

 1) this: to refer current class instance variable
The this keyword can be used to refer current class instance variable. If there is ambiguity between the instance variables and parameters, this keyword resolves the problem of ambiguity.

Example:
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;

It can be used both constructor and method .While executing internally this keyword replaces by object.
Example:
student obj = new student();
obj.rollno=rollno;
If parameter name and instance variable name are different that time java compiler takes care of this keyword.

2) this() : to invoke current class constructor
The this() constructor call can be used to invoke the current class constructor. It is used to
reuse the constructor. In other words, it is used for constructor chaining.

Example:
class A{
A(){
System.out.println("hello a");
}
A(int x){
this(); //calling A() constructor.
System.out.println(x);
}
}

Rules:
1) While using this keyword within constructor it should be fist statement.
2) Never allow multiple this statements inside the constructor.
3) this: to invoke current class method
You may invoke the method of the current class by using this keyword. If you don't use this
keyword, compiler automatically adds this keyword while invoking the method.

Let's see the example
class A{
void m(){
System.out.println("hello m");
}
void n(){
System.out.println("hello n");
this.m(); // same as m()
} }

3) Real usage of this() constructor call
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name){
this.rollno=rollno;
this.name=name;
}
Student(int rollno,String name,float fee){
this(rollno,name);//reusing constructor
this.fee=fee;
}

4) this can be passed as an argument in the method call.
5) this can be passed as argument in the constructor call.
6) this can be used to return the current class instance from the method.

What is Next?
In the next section, we will be going through How to use Methods in Java.

If you enjoyed this post, I’d be very grateful if you’d help it spread by emailing it to a friend, or sharing it on Twitter or Facebook. Thank you! - Shivananda Rai

Comments

  1. Thank you Shivananda Rai...for simple yet effective explanation.

    ReplyDelete

Post a Comment