Java With Us
Home   |   Tutorial   |   Programs  

Java Tutorial

Introduction to Java
Hello World Program
Variables and Data types
More about data types
Displaying text using print and println
Displaying text using printf
Java Comments
Naming conventions for Identifiers
Mathematical operations in Java
Taking input from the user

Classes and Objects
Introduction to object oriented programming
The constituents of a Class
Creating objects and calling methods
Get and Set Methods
Default constructor provided by the compiler
Access Specfiers
Scope and lifetime of Variables
Call by value and Call by Reference

A few more topics
Class as a reference data type
Constants or literals
Final variables
Increment and decrement operators
Manipulating Strings
Overloading constructors and methods
Static methods and variables
The Java API
The Math class
this keyword
Wrapper classes

Control Structures
Control Statements
Repetition statements
Nested loops
Formulating algorithms
Branching Statements

Arrays introduction
Processing arrays using 1oops
Searching and sorting arrays
Array of objects
Multi dimensional arrays
Taking input as command line arguments
Using ellipsis to accept variable number of arguments

Inheritance introduction
Relation between a super class and sub class
Final classes and methods
The protected access specifier
Class Object

Abstract classes and methods

Exception handling
Exception handling introduction
Exception hierarchy
Nested try catch blocks
Throwing exceptions

this Keyword

The this keyword in Java returns a reference to the object on which the method being executed is invoked. We can use the returned reference to access the variables and methods of the object. Look at the following example where the this keyword is used in a constructor

class Student {

   String name;

   public Student ( String name ) { = name;

As we know, a constructor is used to create the object of the class. So, the keyword this returns a reference to the object being created. Therefore, refers to the instance variable 'name' which is assigned with the 'name' passed to the constructor. We could have of course written the constructor as we would have normally written in the following way:

public Student ( String n ) {    name = n; }

But, in this case, a person who is reading the code wouldn't know what n is unless he continues to the next line. So, the use of this keyword is justified in the constructors of a class.

The this keyword is also used to access instance variables that have been hidden by local variables. We have read that a variable can be declared only once. However, there is an exception to this. We can declare an instance variable and a local variable (for example, a variable in a method) with the same name. When we use the variable name in the method body, we get the value which the local variable points at. In effect, the local variable has hidden the instance variable.

public class Student {

   int marks = 34;

   public void newMarks () {
      int marks = 100;
      System.out.println ( marks );

The method newMarks() in the above program would print the integer 100 and not the integer 34. This is because the local variable marks has hidden the instance variable marks. In order the access the hidden variable, we make use of the this keyword in the following way.

System.out.println ( "Old marks: " + this. Marks ); // prints 34

The this keyword has returned a reference to the object and therefore marks accessed through the this keyword refers to the marks that was declared as an instance variable.

The this keyword is also used to call one constructor from another. This is quite helpful in reducing code work when a number of instance variables are declared in a class and there is a necessity to provide a number of constructors which would be able to create objects of that class whether all the necessary parameters have been passed or not. In simpler terms, creation of the object should be made more flexible. The user should be allowed to go ahead, whatever be the information he possesses, be it only the name and rollNumber, just the rollNumber or all the details. Look at the following example of the Student class.

class Student {

   String name;
   int rollNumber;
   int marks1, marks2, marks3;

   public Student ( String name, String rollNumber, int marks1, int marks2, int marks3 ) { = name;
      this. rollNumber = rollNumber;
      this.marks1= marks1;
      this.marks2 = marks2;
      this.marks3= marks3;

   public Student ( String name, int rollNumber ) {
      this ( name, rollNumber, -1,-1, -1 );

   public Student ( String rollNumber ) {
      this ( null, rollNumber );

When we create an object of the Student class in the following way, the second constructor would be invoked which would then invoke the first constructor.

Student s= new Student( "Sai", 34);

And, if we use the following statement, all the three constructors would be called in effect. We are actually making call to the third constructor, which then calls the second which in turn calls the first.

Student s= new Student (34);

An important thing to be remembered when calling other constructors using the keyword this, is that such a call should be the first statement in the constructor. The following code, for instance would result in an error:

public Student () {
   System.out.println ("No argument constructor called"); // incorrect
   this(-1); }

Privacy Policy