This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes CoreJava Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "CoreJava" Watch "CoreJava" New topic
Author

CoreJava

sukhavasi vasavi
Ranch Hand

Joined: Jul 18, 2007
Posts: 59
When this keyword is used?
Shall we define default constructors necessarily,if we defining argument constructor in the extending class
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Not an ORM question. Moving...


JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39478
    
  28
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

    Joined: Oct 13, 2005
    Posts: 39478
        
      28
    . . . and welcome to the Ranch.
    Saathvik Reddy
    Ranch Hand

    Joined: Jun 03, 2005
    Posts: 228
    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 ]
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: CoreJava