• 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

Static method

 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
when I compiled and run the following code I am getting output as "overriding in static2" followed by "in static1" but static methods can be overridden by a static method so in this code s1 is holding the object of static2 so it should call the method of static2 only but why it is calling the method of class static1 please can anybody help me...

class static1
{
protected static void method1()
{
System.out.println("in class static1");
}
}
public class static2 extends static1
{
protected static void method1()
{
System.out.println("overriding");
System.out.println("in static2");
}
public static void main(String args[])
{
static2 s=new static2();
static1 s1=new static2(); //holding the object of static2
s.method1();
s1.method1(); //here it is calling the method of static1 ....
}
}
please help me..
Regards
Krishna
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Krishna,
Static methods cannot be overriden. They can only be hidden. And dynamic binding doesnt apply while hiding. And so the method1 of static1 is bound at compile time itself. And hence the output.
Hope this helps,
Vani.
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Krishna:
Hi,
yes vani is correct.
first thing you should know is never try to override static methods, they are not meant for that there is a specific reason for using static. and that is not for overriding. without creating the object of the class that particular method which is declared as static can be invoked and a static variable is for any instance of the class the value is unique..
so at compile time only method1 is assigned to static1 class and even if you make an instance of other classes static1 will return the same value ie. in this case " in static1"
any one please correct me if you find any mistakes in this..

Rayudu


 
Krishna Shubha
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
Thank you for your good explanation...
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic