aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static variables and methods.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static variables and methods.." Watch "static variables and methods.." New topic
Author

static variables and methods..

geet kaur
Ranch Hand

Joined: Sep 03, 2008
Posts: 78

my question is would x and method in class A be inherited by B?
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
first thing id that your code will not compile because of no return type.

and the second thing is that static method and variable will not be inherited but that will be overhide.


Cleared SCJP 1.5 | Cleared SCWCD 5.0
SCDJWS 6 in progress.....
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Have you compiled this example ?
Methods should have return type.

I think static methods can be inherited. Please check the link given below .

http://www.coderanch.com/t/411599/java/java/static-constructor
[ September 25, 2008: Message edited by: bittoo garg ]
ankit kumar
Ranch Hand

Joined: Apr 28, 2008
Posts: 108
sun page
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
How can static variables be inherited?
Think of it - Static variables are class variables. They belong to class and they have exactly one copy. Each instance created shares that copy. So if it was inherited then this rule would be violated isn't it?
As far as inheriting static methods are concerned, yes they are inherited, but they wont perform the way you think when you try polymorphism. Actually they are hidden.
To learn more, goto SCJP FAQ, and there is a very nice article on this.


Preparing for SCJP 6.0
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234


In the above link .. nothing has been written about inheritence with regard to static methods . This link just explain the difference between overriding and overhiding.



output is "Hi I am static method in parent class"
So I think static methods can be inherited.
V. Potluri
Ranch Hand

Joined: Mar 29, 2007
Posts: 36
Hi,

static members can partcipate in inheritance. But static method cannot be ovverriden. But the rules for overriding a method and hiding a method is same.

Please let me know if my understanding is wrong?

Have a nice day ahead!

Regards,
Gopal
Paul Somnath
Ranch Hand

Joined: May 19, 2008
Posts: 177
Hi All,
Please loot at SCJP FAQ.
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Hi Paul

The link you gave again describe the difference between overriding and overhiding.

The example which I posted in my last post is am example of inheritence.
where subclass object is invoking display function of parent class which is static.
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
Bittoo,
You are talking about OVERRIDING, which deals about DIFFERENTLY-DEFINING a parent class's method in the child class, which is different from just calling a parent class function in the class. You have done the 2nd thing (just a call...You can call a static method of the parent class in your subclass without issues...)

But, in your example, if you override the function display() in your subclass, you will get a compiler error that says something like "you are overriding a static method which is not possible".

Check out the following code for this error:

Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
Bittoo,
You are talking about OVERRIDING, which deals about DIFFERENTLY-DEFINING a parent class's method in the child class, which is different from just calling a parent class function in the class. You have done the 2nd thing (just a call...You can call a static method of the parent class in your subclass without issues...)


I am not talking about overriding.
overriding involves two classes parent and child class .
In my example in my post display method is a static method in parent class only. There is no display method in child class .So no question arises of overriding or overhiding.



But, in your example, if you override the function display() in your subclass, you will get a compiler error that says something like "you are overriding a static method which is not possible".

Check out the following code for this error:


code:
--------------------------------------------------------------------------------

class parent{public static void display(){System.out.println("Hi I am static method in parent class");}}public class subclass extends parent{public void display (){System.out.println ("In subclass");}public static void main(String args[]){subclass s = new subclass();s.display();}}

--------------------------------------------------------------------------------



Yes, You are very true. But this is not my point.

I am just opposing the point that

the second thing is that static method and variable will not be inherited but that will be overhide


I am again posting my example which is not talking about overriding.

Question here is .. can static method be inherited?
I am saying yes. Avobe example clearly shows this.
Bob Ruth
Ranch Hand

Joined: Jun 04, 2007
Posts: 320
I'm having a devil of a time with this one. I have not found Chapter and Verse quoted anywhere in Java documentation that flat out says that static methods are not inherited. What gives me grief proving it is that there are two ways to call a static method a) by class name:

( ClassName.statMethod() ),

or b) using a reference to an object

( objReference.statMethod() ).

Both are legal but the second makes it look like the subclass object inherited the method and it is being called via inheritance.

I'm still looking....
[ September 25, 2008: Message edited by: Bob Ruth ]

------------------------
Bob
SCJP - 86% - June 11, 2009
Rekha Srinath
Ranch Hand

Joined: Sep 13, 2008
Posts: 178
I think Bob has been very clear in putting out this point...
"....but the second makes it look like the subclass object inherited the method and it is being called via inheritance"

So, in Bittoo's original code, the function display() is called using the subclass object, which is very similar to calling the function as parent.display () -- which means its NOT inherited. Its just calling the static method, this time, using a reference which IS-A parent (as opposed to calling the static method using its class)
Anoobkumar Padmanabhan
Ranch Hand

Joined: Aug 08, 2007
Posts: 103
Hi All

See this code also.



Thanks<br /> <br />Anoobkumar<br />SCJP 1.5
Rudolf Meerkotter
Greenhorn

Joined: Apr 03, 2008
Posts: 14
My contribution:


[ September 25, 2008: Message edited by: Rudolf Meerkotter ]

SCJP 5, SCWCD 5, SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static variables and methods..