• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

CoreJava

 
sukhavasi vasavi
Ranch Hand
Posts: 59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When this keyword is used?
Shall we define default constructors necessarily,if we defining argument constructor in the extending class
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not an ORM question. Moving...
 
Campbell Ritchie
Sheriff
Posts: 48652
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The keyword this refers to whichever object you happen to be in at present. Several common uses:-

  • 1: Send a reference to the whole object, eg when serialising . . . objectWriter.writeObject(this);
  • Refer to the attributes corresponding to the object. Example. In a constructor public Foo(String name){ . . . this.name = name;. . .}
  • To get a reference from an inner class object to the object of its surrounding class write "OuterFoo.this"
  • Not absolutely essential, but you can use to to distinguish your present object from another object of the same class . . . if(this.foo.equals(other.foo))
  • And there are bound to be other occasions . . .
     
    Campbell Ritchie
    Sheriff
    Posts: 48652
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    . . . and welcome to the Ranch.
     
    Saathvik Reddy
    Ranch Hand
    Posts: 228
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Originally posted by sukhavasi vasavi:

    Shall we define default constructors necessarily,if we defining argument constructor in the extending class


    Yes!
    [ July 18, 2007: Message edited by: Sheethal Reddy ]
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic