File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static method 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 "static method" Watch "static method" New topic
Author

static method

Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
Why does the compiler complain when we try to override the static method with method that is not static?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Its better if you give the example that you tried or you are referring to... And one more thing, static methods can never be overridden.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


this gives a compile error that static m() cannot be overridden. why?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

hmmm.....sorry for the re question but what do you think are static methods inherited?
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


then why does this work?
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well this is what is called as "redefinition" means defining a different static method for that class. But then you did not answer my question....
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
ok so what I have understood so far is that if I extend a superclass, the subclass inherits the static method which if i want can redefine it. which is the same as overridding. so overiding and redefining are the same

so confusing

Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

First of all, static methods cannot be overridden.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531


the output is "in static"

ofcourse i am inheriting the static method
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Sorry for that... it can be inherited but cannot be overridden.
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
The only thing that has gone into my mind is that when i inherit a static method i cant call this static method of mine using a reference variable of the superclass.
S Ali
Ranch Hand

Joined: Aug 23, 2009
Posts: 129
Or think of it like instance methods cannot hide a static method . Because it can be hidden with another static method.


SCJP 6
Ankur kothari
Ranch Hand

Joined: Sep 06, 2009
Posts: 531
thanks guys for putting that thing in my mind
Atwal Usha
Ranch Hand

Joined: Sep 10, 2009
Posts: 137
Perfect explanation given by S.Ali. Short and simple.


Java Certification Exam Mock Tests: SCJA  SCJP 5  SCJP 6  SCJP 6 (Online Training)  SCJP 6 (Instructor Led Training)   SCWCD 5   SCBCD 5  SCEA 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static method