• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Devaka Cooray
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • paul wheaton
Saloon Keepers:
  • Ganesh Patekar
  • Frits Walraven
  • Tim Moores
  • Ron McLeod
  • Carey Brown
Bartenders:
  • Stephan van Hulst
  • salvin francis
  • Tim Holloway

this and super  RSS feed

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why the "this" and "super" objects of java are non-=static?
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's because they would have no meaning without an instance of the class, an object.

"this" refers to the current instance of that class.
eg super refers to the object upwards the inheritance tree.


 
Marshal
Posts: 61756
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am sure this isn't accurate, but I always think of super as meaning that part of the object which is (directly) inherited from its superclass.
 
Prabz Bhatia
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not completely right.. whenever you instantiate an object, an instance of all objects EDIT: classes upwards the inheritance tree is also generated, automatically. It's a different matter altogether, whether those objects can be independently referenced or not...
 
Sheriff
Posts: 21465
97
Chrome Eclipse IDE Java Spring Ubuntu VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With super, you can access a member (field, class) from the most direct parent class that declares it. So that doesn't have to be the direct superclass if that superclass inherits it from one of its superclasses.
 
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Are static objects contextual ? No.

Are this and super objects contextual ? Yes.



Hope this solves your query

 
Campbell Ritchie
Marshal
Posts: 61756
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does contextual mean, salvin francis?
Are you sure, Prabz Bhatia, that an object is created for each class in its inheritance hierarchy? I did say I thought my answer wasn't accurate.
 
Prabz Bhatia
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:I did say I thought my answer wasn't accurate.


Oh I thought you were talking about my answer...

Campbell Ritchie wrote:
Are you sure, Prabz Bhatia, that an object is created for each class in its inheritance hierarchy?



Yes, I am sure, objects for all non abstract classes upwards the inheritance tree are instantiated whenever an object is created... Proof can be had from the following example


won't work when you try to instantiate B, as B isn't passing a parameter to A's constructor.

It will have to be of the form:


Hope this helps...
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Prabz Bhatia wrote:Yes, I am sure, objects for all non abstract classes upwards the inheritance tree are instantiated whenever an object is created... Proof can be had from the following example



Actually that's wrong. Despite their name, constructors do not create objects, they only initialise them.
A child class will contain all the instance variables defined in the parent class, but an instance of the parent class will not be created, so when you call the parent constructor you are actuall still initialising the child class.
See section 12.5 of the Java Language Specification for full details of what happens when an instance of a class is created.
 
Prabz Bhatia
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Joanne Neal wrote:
Actually that's wrong. Despite their name, constructors do not create objects, they only initialise them.
A child class will contain all the instance variables defined in the parent class, but an instance of the parent class will not be created, so when you call the parent constructor you are actuall still initialising the child class.
See section 12.5 of the Java Language Specification for full details of what happens when an instance of a class is created.



Seems like I sure have gotten that all wrong,
I apologize for the misunderstanding Campbell...
 
Campbell Ritchie
Marshal
Posts: 61756
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Apology accepted

And nice to see you again, Joanne.
 
salvin francis
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
meaning of contextual @ www.thefreedictionary.com

Of, involving, or depending on a context



meaning of context @ www.thefreedictionary.com

The circumstances in which an event occurs; a setting.





what i meant :

"this" has no meaning by itself.
but it has a meaning if you say "'this' within class A" >> it is a reference to class A in current object's context

"super" has no meaning by itself.
but it has a meaning if you say "'super' within class B extending A" >> it is a reference to class A in current object's context

thus the above two words have meaning in context (of the object from which they are called).

but

we can directly says



The variable objectCount is accessed without any reference to any object of class A (without context)


 
Campbell Ritchie
Marshal
Posts: 61756
193
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

salvin francis wrote:meaning of contextual . . .

Thank you very much. It is a word I have never before seen used in that particular context
 
salvin francis
Bartender
Posts: 1983
57
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!