• 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

Overriding static method

 
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
According to K&B SCJP 5 guide (pg. 147); static methods can't be overridden.
They can be redefined however, but not overridden. So far so good.
Next, I created a little sample project with following structure:





Now I get a warning on //line 1;
main(java.lang.String...) in package_b.Sedan overrides main(java.lang.String[]) in package_a.Convertible; overridden method has no '...'

So where does this warning come from ? Is it varargs- or compiler-related ? If I use the 'normal' (String[] args) - syntax the warning disappears...
I'm working with NetBeans 6.1 & Java 6.
 
Vaes Bart
Greenhorn
Posts: 13
Netbeans IDE Opera Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Nvm, found the explanation in "hiding vs overriding"...
 
Sheriff
Posts: 9691
42
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
well this is confusing for a lot of people... Look at this code



The above code will give you error that you cannot override a static method. So basically you are overriding the static method. But it is actually not being overridden. Look at this code

class Super
{
static void method()
{
System.out.println("Super");
}
}
class Sub extends Super
{
static void method()
{
System.out.println("Sub");
}
}
class MainClass
{
public static void main(String[] args)
{
Super sup = new Sub();
sup.method(); //will display Super
}
}[/CODE]

So as you can see that static methods don't behave like non-static methods. Their call depends on the reference being used to call them. They behave just like fields....
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic