|
![]() |
Lovin' java
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Ankit Garg wrote:I'm not able to understand what you mean
You can access super class static methods using super keyword. This is only not allowed in static methods. This is because super and this are available only in non-static methods...
Anbarasu Aladiyan
anbarasu aladiyan wrote:Both 'this' and 'super' used with instances NOT WITH STATIC. So we can not use 'super' keyword to access a static method. We can use class name to access STATIC method.
When super.method() is called, the compiler gets the SuperClass and invokes the method.Java language also allows you to use an object reference variable to access a static member.This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member.
Sachin Adat wrote:
anbarasu aladiyan wrote:Both 'this' and 'super' used with instances NOT WITH STATIC. So we can not use 'super' keyword to access a static method. We can use class name to access STATIC method.
That's not right. We can use super to access a static method.
From K&B
When super.method() is called, the compiler gets the SuperClass and invokes the method.Java language also allows you to use an object reference variable to access a static member.This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member.
That's what the code snippet from Ankit proves. Its not good practice to call a static method that way, but it is surely legal though.
Java language also allows you to use an object reference variable to access a static member.This is merely a syntax trick to let you use an object reference variable (but not the object it refers to) to get to a static method or variable, but the static member is still unaware of the particular instance used to invoke the static member.
Anbarasu Aladiyan
anbarasu aladiyan wrote:this means we can use object reference of the class to access its static method.
That's exactly what we are talking about, and Ankit also mentioned, that we cannot use super and this from a static method.anbarasu aladiyan wrote:IT does not means we can use super keyword in static method. Always 'super' and 'this' must not be used from static method
Sachin Adat wrote:
anbarasu aladiyan wrote:this means we can use object reference of the class to access its static method.
Hence we can use super from a non-static method to call a superclass static method
That's exactly what we are talking about, and Ankit also mentioned, that we cannot use super and this from a static method.anbarasu aladiyan wrote:IT does not means we can use super keyword in static method. Always 'super' and 'this' must not be used from static method
I am not saying that you can call super or this from a static method. What I tried to tell was that if you want to call a static method from subclass static method you have to use the super class name 'Super' (watch the case) in the example to invoke the static method or you can also call the method directly since subclasses have access to Superclass static methods.
Anbarasu Aladiyan
anbarasu aladiyan wrote:Could you please go through your message posted on today (07-07-09 ) at 4:07:52 PM
Ranch Hand
Joined: Sep 25, 2007
Messages: 41
[Post New]posted Yesterday 11:26:40 PM private message
Quote [Up]
And static members are NOT inherited so therefore, they CAN'T be overriden. Overriden applies only to those inherited methods
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Dhrubajyoti Chatterjee wrote:I agree that static method cannot be overridden in subclass but can you please explain the following code;
This produces the following compilation error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot override the final method from Base
at Derived.method(test.java:12)
at test.main(test.java:21)
Can you please explain in base class static method is not overridden, then why is compilation failing when i redefine the method in subclass.
Also if I remove the final keyword from base class method declaration, the code compiles fine.
Adolfo Eloy
Software Developer
OCPJP 6
Dhrubajyoti Chatterjee wrote:
Can you please explain in base class static method is not overridden, then why is compilation failing when i redefine the method in subclass.
Also if I remove the final keyword from base class method declaration, the code compiles fine.
Dhrubajyoti Chatterjee wrote:I agree that static method cannot be overridden in subclass but can you please explain the following code;
This produces the following compilation error:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
Cannot override the final method from Base
at Derived.method(test.java:12)
at test.main(test.java:21)
Can you please explain in base class static method is not overridden, then why is compilation failing when i redefine the method in subclass.
Also if I remove the final keyword from base class method declaration, the code compiles fine.
*** If opportunity doesn't knock, build a door !! ***
Yeah, but is it art? What do you think tiny ad?
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
|