• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

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

 
Ranch Hand
Posts: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 8520
615
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 179
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 1059
27
Netbeans IDE Oracle MySQL Database Tomcat Server C++ Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 373
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 179
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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?
     
    Marshal
    Posts: 76856
    366
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
    the value of filler advertising in 2021
    https://coderanch.com/t/730886/filler-advertising
    reply
      Bookmark Topic Watch Topic
    • New Topic