• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static, super & this

 
geetanjali rajput
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3271
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
geetanjali rajput
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks corey ,really.
 
aminur rashid
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic