aspose file tools*
The moose likes Beginning Java and the fly likes this and super Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this and super" Watch "this and super" New topic
Author

this and super

Ramm Kummar
Greenhorn

Joined: Feb 10, 2009
Posts: 26


Why the "this" and "super" objects of java are non-=static?
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
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.


Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Apr 14, 2009
Posts: 19
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...
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928


Are static objects contextual ? No.

Are this and super objects contextual ? Yes.



Hope this solves your query


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

Joined: Oct 13, 2005
Posts: 38363
    
  23
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

Joined: Apr 14, 2009
Posts: 19
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...
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3446
    
  12
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.


Joanne
Prabz Bhatia
Greenhorn

Joined: Apr 14, 2009
Posts: 19
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
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
Apology accepted

And nice to see you again, Joanne.
salvin francis
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

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
Sheriff

Joined: Oct 13, 2005
Posts: 38363
    
  23
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
Ranch Hand

Joined: Jan 12, 2009
Posts: 928

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this and super