• 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
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

This and Super keyword

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Sheriff
Posts: 67753
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
this and super. Case counts!
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you have been given examples of most of their uses, and welcome to the Ranch
 
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Checkout another usage of "this" keyword:


 
Campbell Ritchie
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you need BaseClass.this there? I thought you only used ClassName.this inside inner or nested classes.
 
Prashant Ameta
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2911
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 80295
434
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
It's just like a fortune cookie, but instead of a cookie, it's pie. And we'll call it ... tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic