*
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 Murach's Java Servlets and JSP this week in the Servlets 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: 37865
    
  22
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: 37865
        
      22
    . . . 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 ]
     
     
    subject: CoreJava
     
    Similar Threads
    Developing our own Exam Simulator?
    DO GOD STILL EXIST IN THE WORLD
    Outgrowing our server(s)!
    chatting option
    ejb-jar file 's content