• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

overridding instance method with static method

 
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi i tried this
public class B {
public static String toString()
{
return "HI";
}
public static void main(String s[])
{
System.out.println(new B());
}
} //B class ends
when comiled::::::::::::::::
B.java:4: Instance methods can't be overridden by a static method.
Method java.lang.String toString() is an instance method in class java.lang.Obje
ct.
public static String toString()
^
1 error

can anyone explain me this
Thanks in advance....
------------------
Sachin,
****************************************************
Learn from others mistakes. Life is too short to make all yourself.
****************************************************
 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try to change your method to public String toString() instead of
public static String toString(). You can't override a instance method with a static method.
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vice-versa is also true. You cannot override a static method to be non-static.
Ajith
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The compiler error is self-explanatory:
you simply cannot override an instance method with a static method. That is why you get the error.
The toString() method in class Object is defined as
public String toString()
and thus cannot be overriden with
public static String toString()
 
sachin patel
Ranch Hand
Posts: 75
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you very much....
But i was just wondering if you can throw more light on this because i read this somewhere and i dont get it... may be little explanation will help....
Thanks in advance
------------------
Sachin,
****************************************************
Learn from others mistakes. Life is too short to make all yourself.
****************************************************
 
reply
    Bookmark Topic Watch Topic
  • New Topic