this keyword in java with example

this keyword in java is used to refer to current object or instance of class. It can be used in the constructor to call any other overloaded constructor but this keyword should be the first statement in the constructor.

This keyword can be used for instance variables

this keyword can be used to refer to the instance variable of class.

When you run above program, you will get below output:

Employee’s name : John
Employee’s age : 20

As you can see we have used this keyword to set values for instance variable in the constructor.

This keyword can be used to call the overloaded constructor

If you want to call overloaded constructor of same class, you can use this keyword to do that.
For example:

When you run above program, you will get below output:

Calling No arg constructor
Calling Parameterized constructor
Employee’s name : John
Employee’s age : 20

Please note that this keyword used for calling another constructor should be first statement in that constructor.

this keyword can be used return object of the class

Calling No arg constructor
Employee’s name : John
Employee’s age : 20

that’s all about this keyword in java.

Related Posts

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • Break out of nested loop in java
    22 July

    How to break out of nested loops in Java

    Table of ContentsUsing break (will break inner loop)Using named loopUsing named blockUsing returnConclusion In this post, we will see how to break out of nested loops in Java. Using break (will break inner loop) It is very important to understand how nested loops work to ensure that applying break will output the desired result. If […]

  • public static void main(String args[]) - Java main method
    11 June

    public static void main(String[] args) – Java main method

    Table of ContentsWhy is the main method so important?SyntaxExplanation of the KeywordsExamples of Java main method ProgramsSimple Java ProgramPassing arguments in the main methodNon-Public main method.Defining a return value for the main methodOverloading the main methodQuestionsCan we run java without main method?Can we have more than one main method in classConclusion If you have worked […]

  • Java projects for beginners
    30 April

    Top 20 Java Projects for Beginners

    This tutorial provides the top 20 java projects for beginners for practice. These projects will help you to learn java quickly and efficiently.

  • How to end program in java
    08 March

    How to end program in java

    Table of ContentsUsing System.exit() to end java programExample to demonstrate System.exit() methodHow to end java program in an if statement in javaUsing return to end current method Ho In this post, we will see how to end program in java. You can use System.exit(0) to end program in java. Using System.exit() to end java program […]

  • 25 September

    Reference Variable in Java

    Table of ContentsReference variableStatic Reference VariableInstance Reference VariableLocal Reference Variable In this post, we will see about Reference variable in java. Reference variable A variable that holds reference of an object is called a reference variable. Variable is a name that is used to hold a value of any type during program execution. If the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.