
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Is it possible to assign a reference to "this" in java?
The "this" keyword in Java is used as a reference to the current object, within an instance method or a constructor. Using it, you can refer the members of a class such as constructors, variables, and methods.
Assigning reference to "this"
According to the definition "this" is a keyword which acts as a reference to the current object (the object from whose constructor/method you are using it), its value id is fixed. Therefore, you cannot assign a new reference value to it. Moreover, it is just a keyword, not a variable.
Still, if you try to it assign a reference value to "this" it leads to a compilation error.
Example
In the following Java program, the class (ExampleClass) has two private variables name, age and, a parameterized constructor which instantiates these variables. From a method named display, we are trying to assign a new value to "this".
public class ExampleClass { private String name; private int age; public ExampleClass(String name, int age){ this.name = name; this.age = age; } public void display(){ this = new ExampleClass("krishna", 23); } }
Compile-time error
On compiling, this program gives you an error as shown below −
ExampleClass.java:14: error: cannot assign a value to final variable this this = new ExampleClass("krishna", 23); ^ 1 error
- Related Articles
- Is it possible to assign a numeric value to an enum in Java?
- Reference assignment operator in PHP to assign a reference?
- Is it possible to use this keyword in static context in java?
- How to assign a reference to a variable in C#
- What is ‘this’ reference in Java?
- Can we assign a reference to a variable in Python?
- Is it possible to instantiate Type-parameter in Java?
- Is it possible to create static constructor in java?
- Is it possible to create a class without a name in Java?
- Is it possible to use $(this) and universal selector (*) with jQuery?
- Is it possible to synchronize the string type in Java?
- 'this' reference in Java\n
- Is it possible to override a Java method of one class in same?
- Is it possible to check if a String only contains ASCII in java?
- Assign other value to a variable from two possible values in C++
