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

Popular posts from this blog

How to Install Eclipse and Get Started with Java Programming

  #Topic-3 How to Install Eclipse and Get Started with Java Programming. Last topic we have discussed about Java Development Kit (JDK) tool and how to write, compile & run (execute) a java program using notepad and command prompt.If you not go through that ,click this link What is the software’s needed to start JAVA programming . So let’s see How to Install Eclipse and Get Started with Java Programming.   What is Eclipse? Eclipse is famous for Java Integrated Development Environment (IDE), Eclipse is an open-source Integrated Development Environment (IDE). Eclipse is popular for Java application development (Java SE and Java EE) and Android apps. It also supports C/C++, PHP, Python, Perl, and other web project developments via extensible plug-ins. Eclipse is cross-platform and runs under Windows, Linux and Mac OS. The Java Development Tools (JDT) project provides a plug-in that allows Eclipse to be used as a Java IDE, PyDev is a plugin that allows Ecli...

What is the software’s needed to start JAVA programming

#Topic-2 Last week we have discussed about some basic topics like how to start java , what is the use etc , If you are very new to Java programming then click this link Java Tutorials for Beginners. What is the software’s needed to start JAVA programming. If you are new to Java programming and wish to learn it right now by doing some hands-on practice, you have visited the right place. This tutorial will help you what is the software’s needed to write your first Java program, Throughout this tutorial, you will learn fundamental concepts and steps which are necessary for every Java fresher.  In order to write and run a Java program, you need below software / editor: 1. Java Development Kit (JDK) 2. Command prompt 3. Note pad OR 4.IDEs : These IDEs offer a variety of features, like: building Java applications, Testing, debugging, code inspections, code assistance, visual GUI builder and code editor, and more. While you can find several Java IDEs or integrated developmen...

Variables and Data Types in Java

#Topic-5 Previous topic (4) we have discussed about how to start coding! , we have gone through our first Java Program line by line. If you have not gone through that topic, click this link  Your First Java Program: Hello World In this section we will discuss about variable , Data types , Literals and many more! Variable Variable is name of reserved area allocated in memory . In other words, it is a name of memory location . It is a combination of "vary + able" that means its value can be changed.   Example:   int value= 10 ; //Here value is variable float a= 10.50 ; //Here a is variable   There are three major types of variables in java: local variable instance variable static variable 1) Local Variable : A variable which is declared inside the method is called local variable. 2) Instance Variable : A variable which is declared inside the class but outside the method, is called instance variable . It is not declare...