Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

THIS keyword and overloaded constructors -- which one does the compiler know to execute  RSS feed

 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two questions here:

Can someone explain to me exactly what the 'this' keyword does? I seem to be at a loss to understand the definitions I've read so far; rather confusing.

As well, when you have overladed constructors in a class, how does the compiler know which one is called?
 
Liutauras Vilda
Marshal
Posts: 4650
318
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:Can someone explain to me exactly what the 'this' keyword does? I seem to be at a loss to understand the definitions I've read so far; rather confusing.
Have you tried to read this? (<- link). I don't think I could explain it clearer. Give a try and tell us if you still have doubts.

Christopher Laurenzano wrote:when you have overladed constructors in a class, how does the compiler know which one is called?
You know by the passed arguments to the constructor during the object creation. Each declared constructor has different parameters list (or don't have at all). So, constructors parameters list (with types) has to match with passed arguments.
 
Christopher Laurenzano
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, but I still don't get it. To be honest, I don't think it's written that clearly. But in either case I'm still confused.
 
Les Morgan
Rancher
Posts: 768
19
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Christopher Laurenzano wrote:Sorry, but I still don't get it. To be honest, I don't think it's written that clearly. But in either case I'm still confused.


"this" refers to the active (currently executing code) object, so in the examples there were arguments of x and y for the method, where it says this.x = x means the x of the object gets the value of the argument and the same for this.y = y.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Christopher

First of all mind this Java is case sensitive so "THIS"(as mentioned in the title of this post) is not a keyword ,"this" is a keyword.

"this" can be used in 2 places :

  • inside instance method
  • inside constructor


  • Use:
  • "this" is used as a reference to current object.


  • It can be used to refer any member of that instance.


  • Its two main uses occurs :
  • During data shadowing
  • During Explicit constructor invocation








  • Now the second part of your question,
    First mind this:Compiler tries to find all the error that can be generated in the code ,without even executing a Single line

    And the argument list you provide while creating instance will help in identification of particular constructor
     
    Christopher Laurenzano
    Ranch Hand
    Posts: 105
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sorry, but I still don't understand what's going on and/or which constructor's being called. Can you go through the logic?
     
    Campbell Ritchie
    Marshal
    Posts: 55751
    163
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I think the tutorial Liutauras linked to should explain it to you. But they keyword this has three uses:
    As this on its own it means the whole of the object you happen to be working in.
    As this. with a dot it means the instance member rather than any local variables.
    As this(...); with round brackets it is a constructor call from one constructor to another. It must be first in the constructor. The compiler disambiguates overloaded constructors the same way as for methods: look at the type and number of arguments. If you write
    this(123, 456, 789);
    it will look for a constructor which can take three ints or maybe three longs or int... or a three Integers as parameters. If there is any confusion it looks for which it thinks the more specific. If it can't sort them out it will fail to compile. The details are in the Java® Language Specification (=JLS), but that can be very difficult to read. There is a link in the JLS to its §15.9 which will explain how it is resolved. That is bit will be difficult to understand, I expect.
     
    It is sorta covered in the JavaRanch Style Guide.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!