wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes static, super & this Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static, super & this" Watch "static, super & this" New topic
Author

static, super & this

geetanjali rajput
Greenhorn

Joined: Apr 02, 2002
Posts: 19
please give me as much examples as u can on static, super & this. try the best and simple way.i will really appreciate your help.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
Well, static is a little different from super and this, but I'll try to explain them all.
First of all, let's talk about static. The keyword static is generally used to refer to something that is "class specific" rather than "object specific." For example, if you define a member to be static, that member belongs to the class, rather than to any specific object of that class. Let's look at an example.

As you can see from this code, the color of each box is an instance variable. It's value is different for every instance.
The static variable numBoxesCreated, however, is often referred to as a "class variable." This member (since it is declared as static) is the same for all objects. In fact, this member exists without any objects being created at all. When you declare something as static, it belongs to the class, not to an instance of that class.
Okay, now let's look at this and super. The keyword this refers to the object you're currently in. The keyword super refers to the object that the object you're currently in inherits from.
Take this example:

By using the keywords this and super, you can explicitly refer to the object you're in or the object this object inherits from. This concept becomes increasingly useful when you're using complicated inheritance models with lots of method over-riding and shadowing.
I hope this helps a little.
If you have more specific questions, I'd be happy to help.
Corey


SCJP Tipline, etc.
Corey McGlone
Ranch Hand

Joined: Dec 20, 2001
Posts: 3271
In addition, you might want to check out the following sections of the JLS:
this
§15.8.3 this
§15.8.4 Qualified this
super
§15.11.2 Accessing Superclass Members using super
§15.12.4.9 Example: Method Invocation using super
static
§8.3.1.1 static Fields
§8.4.3.2 static Methods
§8.5.2 Static Member Type Declarations
§8.7 Static Initializers
§15.12.4.6 Example: Target Reference and Static Methods
§16.7 Definite Assignment and Static Initializers
geetanjali rajput
Greenhorn

Joined: Apr 02, 2002
Posts: 19
thanks corey ,really.
aminur rashid
Ranch Hand

Joined: Sep 21, 2001
Posts: 79
Originally posted by Corey McGlone:

Take this example:

By using the keywords this and super, you can explicitly refer to the object you're in or the object this object inherits from. This concept becomes increasingly useful when you're using complicated inheritance models with lots of method over-riding and shadowing.
Corey

Hey when i compiled ur code it gave the following error

No when i declared the method doIt() as static
it gave the error

How to go ahead??


Regards<br />Aminur
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: static, super & this
 
Similar Threads
Overridden methods
question from master exam
variable this is undefined ?
this Refrence Problem
this and super