Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Is it possible to overload static methods ?

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ?

 
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[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"?
 
Ranch Hand
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
     
    Mauricio Lopes
    Greenhorn
    Posts: 12
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    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 !
     
    author
    Posts: 9031
    21
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    cool!

    I'd love to see a portuguese version of the book!
     
    Ranch Hand
    Posts: 46
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    This topic is interesting. K & B does mention about hiding.
     
    With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
    reply
      Bookmark Topic Watch Topic
    • New Topic