This week's book giveaways are in the Java EE and JavaScript forums.
We're giving away four copies each of The Java EE 7 Tutorial Volume 1 or Volume 2(winners choice) and jQuery UI in Action and have the authors on-line!
See this thread and this one for details.
The moose likes Beginning Java and the fly likes This and Super keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "This and Super keyword" Watch "This and Super keyword" New topic
Author

This and Super keyword

Prashant Ameta
Greenhorn

Joined: Jul 17, 2012
Posts: 11
Hii , m quite new to the Java world and m looking for some very general examples,actually explaination for "This" and "Super" key words!!!i hav a book but it looks very BOOKISH

Thanks and Regards
Prashant Ameta
John McClellan
Ranch Hand

Joined: Aug 15, 2011
Posts: 35
If you're really new to programming in general, not just Java, this may be a topic you'll want to cover later. this refers to the object that the method using that keyword is a part of, and in a way, so does super. However this uses the regular class basically, whereas super uses the base class.

Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61066
    
  66

this and super. Case counts!


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1648
    
    7

this - a final reference to the current object. Can be used in any non-static context for example inside the body of an instance method.

there is also the this() construct. This is useful for overloading constructors. this() must be the first statement in a constructor and will invoke the local constructor with a corresponding parameter list.

super() is used in a subclass to invoke the superclass constructor with a corresponding parameter list. super() must also be the first statement in a constructor.

So in short this() is used for chaining local constructors when super() chains subclass constructors to superclass constructors.


[How To Ask Questions][Read before you PM me]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
I think you have been given examples of most of their uses, and welcome to the Ranch
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Checkout another usage of "this" keyword:




My Website: [Salvin.in] Cool your mind:[Salvin.in/painting] My Sally:[Salvin.in/sally]
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Do you need BaseClass.this there? I thought you only used ClassName.this inside inner or nested classes.
Prashant Ameta
Greenhorn

Joined: Jul 17, 2012
Posts: 11
thanks alot BEar,Sherrif and Mr.Gorder
thanks a ton JOhn and Salvin....thats exactly what i was looking for.
and now i know how to ask questions...

salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

Campbell Ritchie wrote:Do you need BaseClass.this there?

Yes, since we want to access the someVariable from BaseClass

Campbell Ritchie wrote:I thought you only used ClassName.this inside inner or nested classes.

My program works


Checkout this program:


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
salvin francis wrote: . . . My program works . . .
Wrong response.

The correct response is to point out what I missed, that you are actually inside an inner class. Sorry for my mistake.
 
jQuery in Action, 2nd edition
 
subject: This and Super keyword