aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Super in static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Super in static" Watch "Super in static" New topic
Author

Super in static

Rahul Singhai
Greenhorn

Joined: Mar 04, 2008
Posts: 19
Ranchers,
vert silly doubt..
Can we use super() in static memebers?

Thanks
Rahul


<br />
SCJP - 93%
Warm Regards,<br />Rollicking
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ?


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Rahul Singhai
Greenhorn

Joined: Mar 04, 2008
Posts: 19
I cant try it now. I dont have a a PC nearby.However I feel i should get a error.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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....


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Mar 04, 2008
Posts: 19
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

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: May 19, 2008
Posts: 177
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 ]

Preparing for SCJP 6.0
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9302
    
  17

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

Joined: Oct 13, 2005
Posts: 38818
    
  23
Please have a look at some threads from another forum discussing similar problems: 1 2 3.
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
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

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 !
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Super in static