• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Super in static

 
Rahul Singhai
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ranchers,
vert silly doubt..
Can we use super() in static memebers?

Thanks
Rahul
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Ankit Garg
Sheriff
Posts: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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
  • 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: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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....
 
Paul Somnath
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • 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: 9529
33
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • 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 ]
 
Campbell Ritchie
Sheriff
Pie
Posts: 50277
80
  • Mark post as helpful
  • send pies
  • 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
  • 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: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • 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 !
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic