Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Is it possible to overload static methods ? 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 "Is it possible to overload static methods ?" Watch "Is it possible to overload static methods ?" New topic
Author

Is it possible to overload static methods ?

Mauricio Lopes
Greenhorn

Joined: Aug 04, 2009
Posts: 12
Reading S&B for Java 5, in the end of chapter 2 there is the following statement:

"static methods cannot be overloaded".

But testing it on my IDE i've written the following valid code:


That's an overloading or am I wrong ?

pete stein
Bartender

Joined: Feb 23, 2007
Posts: 1561
[quote=Mauricio Lopes]Reading S&B for Java 5, in the end of chapter 2 there is the following statement:

"static methods cannot be overloaded".

[/quote]

Are you sure it states "cannot be overloaded" and not "cannot be overridden"?
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

you are misunderstood a little bit here
static methods can definitely be overloaded
but they cannot be overridden
means
you can write static methods with the same name and the same method signature in two different classes but that is not
called as method overriding
this is called as method hiding
as in inheritance the static method defined in the subclass actually hides the method in the super class
burn following rules into the mind
  • Static methods cannot be overridden, that is called as method hiding

  • If super there is a static method in super class called as doStuff(), then in subclass we have to declare the method with name doStuff() as static
    this is mandatory other compilation fails

    the example of method hiding is as follows

    This outputs as follows


    If you are still unclear about the overloading and overriding of methods
    then I recommend you should go through the K & B thoroughly to make it fully understand
    happy preparation


    SCJP 6 [86%] June 30th, 2010
    OCPWCD [84%] March 26th, 2013
    If you find any post useful, click the "plus one" sign on the right
    Mauricio Lopes
    Greenhorn

    Joined: Aug 04, 2009
    Posts: 12
    That's amazing ! What a great help !

    Oh yeah, it's written in capital letters "MÉTODOS STATIC NÃO PODEM SER SOBRECARREGADOS." (static methods cannot be overloaded). I've got a portuguese version of that book, perhaps it's mistranslated. It says that it can be hidden by another implementation, as you explained.

    Thanks a lot !
    Bert Bates
    author
    Sheriff

    Joined: Oct 14, 2002
    Posts: 8815
        
        5
    cool!

    I'd love to see a portuguese version of the book!


    Spot false dilemmas now, ask me how!
    (If you're not on the edge, you're taking up too much room.)
    KrishnaPrasad raghavan
    Ranch Hand

    Joined: Oct 28, 2008
    Posts: 46
    This topic is interesting. K & B does mention about hiding.
     
    It is sorta covered in the JavaRanch Style Guide.
     
    subject: Is it possible to overload static methods ?