• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Super in static

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ranchers,
vert silly doubt..
Can we use super() in static memebers?

Thanks
Rahul
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rahul Singhai:
Ranchers,
vert silly doubt..
Can we use super() in static memebers?

Thanks
Rahul



The best answer ll be , have you tried it ?
 
Rahul Singhai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I cant try it now. I dont have a a PC nearby.However I feel i should get a error.
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No you cant use super in static....

Originally posted by Rahul Singhai:
I cant try it now. I dont have a a PC nearby.However I feel i should get a error.



Say you don't have a PC with Java in it....are you posting you problem from a futuristic equipment which is not a PC and about which we don't know....
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
static means it totally class thing, one class static members are not derived to child class , so Whts the use of super() calling in static method..
 
Rahul Singhai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
you are very right Ankit.. Sorry.. i would have made it clear.
I am in Library. I do have PC but it hasnt Java. Even i cant install as well as it is for public use only.
anyways.
Thanks..
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its OK man I was also joking .....I don't get too many opportunities to crack some jokes so this time when I got the opportunity I cracked one....
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar Rohankar:
static means it totally class thing, one class static members are not derived to child class , so Whts the use of super() calling in static method..



If static base class static members are not derived to child class, then how about the following code:



In this code, both the static member and method are accessible to the sub-class. I have made use of protected access modifier to show that the the member var and method MyMethod are accessible in sub-class.

As far as I know, super() is the call to the super-class constructor and this is allowed only in the first line of any sub-class constructor. And since a class constructor deals with object creation, it cannot be static. Also any method which is static cannot have the super(); statement in the method since super() is allowed only in constructors.
[ August 28, 2008: Message edited by: Somnath Paul ]
[ August 28, 2008: Message edited by: Somnath Paul ]
 
Ankit Garg
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well many people have confusion with this...actually static members are inherited...that's why you can refer to them directly by their names(if they are not hidden). The only thing is that you can't override them. You can however hide them... Let me explain this



The output is A because after compilation the compiler replaces the reference name with the of the reference. So main after compilation would look like this


This is why you can even do this

or this


I hope this will make things a bit clear....
[ August 28, 2008: Message edited by: Ankit Garg ]
 
Marshal
Posts: 79273
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please have a look at some threads from another forum discussing similar problems: 1 2 3.
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think the most exhaustive information about this can be found here: http://faq.javaranch.com/view?OverridingVsHiding.
[ August 28, 2008: Message edited by: Somnath Paul ]
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Somnath Paul:


If static base class static members are not derived to child class, then how about the following code:



In this code, both the static member and method are accessible to the sub-class. I have made use of protected access modifier to show that the the member var and method MyMethod are accessible in sub-class.


Sory, My mistake, I understood now !
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic