Topic:   Can we assign the reference to this variable?
Feb 24, 2021 11:29 1 Replies 96 Views ROHAN

Can we assign the reference to this variable?

Prev Next
Topic Replies (1)
  1. 1
    idnkx user

    RANJIT

    No, this cannot be assigned to any value because it always points to the current class object and this is the final reference in Java. However, if we try to do so, the compiler error will be shown. Consider the following example.
    1. public class Test
    2. {
    3. public Test()
    4. {
    5. this = null;
    6. System.out.println("Test class constructor called");
    7. }
    8. public static void main (String args[])
    9. {
    10. Test t = new Test();
    11. }
    12. }
    Output
    Test.java:5: error: cannot assign a value to final variable this
    this = null;
    ^
    1 error

Leave a Reply
Guest User

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect