About overriding static method inheritance

I have some class PO2VOPropertyUtilsBean which extends the org.apache.commons.beanutils.PropertyUtilsBean.
In PropertyUtilsBean,there is a method:
protected PropertyUtilsBean getInstance() {}I override it in my extend calss PO2VOPropertyUtilsBean:
protected PO2VOPropertyUtilsBean getInstance() {
      return new PO2VOPropertyUtilsBean();
}I use Eclipse3.1+Myeclipse4.0,when i choose java5.0 as complier,that all be ok.
But when i copy the source to my company, my pc uses java1.4 as compiler,it show me errors in the getInstance() method:
imcompartible return type,it should return PropertyUtilsBean.
Why?

Ok, so the getInstance() methods are indeed static.
As Kaj said, static methods cannot be overridden.
You would call the method by the classname. For example:
PropertyUtilsBean bean = PropertyUtilsBean.getInstance();If you write a class PO2VOPropertyUtilsBean with a getInstance() method, the call above will still go to class PropertyUtilsBean, and not to your derived class. After all, how is Java supposed to know that you want to have the method in PO2VOPropertyUtilsBean called? You would need to change the call:
PropertyUtilsBean bean = PO2VOPropertyUtilsBean.getInstance();

Similar Messages

  • Overriding static method

    hi all
    can be override static method.if yes then how?plz explain.

    Static methods do hide rather than override - the superclass-and-above methods are, however, visible via explicit referencing. Example:
    public class Foo
         public static final void main(String[] args)
              Foo.foo();
              Poo.foo();
              Foo.bar();
              Poo.bar();
         public static void foo()
              System.out.println("Foo foo");
         public static void bar()
              System.out.println("Foo bar");
         public static class Poo extends Foo
              public static void foo()
                   System.out.println("Poo foo");
              public static void bar()
                   System.out.print("Poo bar, calling foo(): ");
                   foo();
    }Gives
    Foo foo
    Poo foo
    Foo bar
    Poo bar, calling foo(): Poo fooAlways more interesting to try stuff and just see what happens, don't you think? :o)
    Message was edited by:
    itchyscratchy - line wrap pasting error

  • Can we override Static methods?

    Hi all, I m in little bit confusion abt static methods overriding.
    Could you help me on this issue.,
    Can we override Static methods?

    You can provide static methods with same name in both super class and subclass. But it is not overriding in this scenario. Both will act as independent methods.
    Example:
    have a super class:
    public class Test1 {
    public static void mthdA()
    System.out.println("in super");
    have a sub class:
    public class Test2 extends Test1{
    public static void mthdA()
    System.out.println("inside sub");
    public static void main(String[] args){
    Test1 objTest = new Test2();
    objTest.mthdA();
    Try to run the sub class Test2. The output would be "in super".
    Remove static modifier in mthdA() in both the classes and then run. The output would be "in sub". Meaning, methdA is overriden in sub class.

  • Querry regarding overriding static methods

    Hi ,
    I m overriding a static method defined in superclass ThreadLocal1 as:
    public static void mm()
         System.out.println("bye");
    class Thread1 extends ThreadLocal1{
    tt(){}
    public static void mm()
         System.out.println("hi");
    public static void main(String args[])
         Thread1 t1= new Thread1();
         ThreadLocal1 th1;
         th1=t1;          // t1 is assigned      th1.mm();
         t1.mm();
    }output is : bye
    hi
    I am not getting why ref var th1 is not calling overridden method mm() when th1 is assigned t1 that is subclass instance.
    when i m removing static modifier from mm(0 in superclass then output is : hi hi
    plz clarify why is it so.....
    Edited by: jawatch on Oct 29, 2007 7:01 AM

    jawatch wrote:
    @kajbj :
    but i have overridden static method mm()No, you haven't. You've hidden it. Overriding only applies to non-private, non-final, non-static methods, and they are the only case where you can see the kind of polymorphism you're talking about.

  • Redefine static method?

    It seems that I cannot redefine a static method in a subclass in ABAP OO, right?
    Is there a reason for that? Is this like this in other languages as well?

    That's true. You cannot redefine static methods in ABAP OO.
    I can add that a class that defines a public or protected static attribute shares this
    attribute with all its subclasses.
    Overriding static methods is possible for example in Java.
    This simple piece of code illustrates this:
    public class Super {
        public static String getNum(){
            return "I'm super";
         public static void main(String[] args) {
             System.out.println("Super: " + Super.getNum());
             System.out.println("Sub: " + Sub.getNum());
    public class Sub extends Super{
        public static String getNum(){
            return "I'm not";
    The output is:
    Super: I'm super
    Sub: I'm not
    When overriding methods in Java you must remember that an instance method cannot override a static method, and a static method cannot hide an instance method.
    In C# a static member can't be marked as 'override', 'virtual' or 'abstract'. But it it is possible to hide a base class static method in a sub-class by using the keyword 'new':
    public class Super
      public static void myMethod()
    public class Sub: Super
      public new static void myMethod()

  • Static methods in interfaces

    Java cognoscenti,
    Anyone know why I can't declare a method as static in an interface, but I can in an abstract class. Surely, semantically it's the same - defering the implementation of a static method, not an abstract class and an interface. By the way, I'm using JDK 1.2.2 if that makes a difference

    No, it's not the same. You are not "defering the
    implementation of a static method" because static
    methods are never polymorphic.
    Static methods cannot be abstract for this reason, and
    only abstract methods are allowed in interfaces.I didnt't know that. I thought that - because you can override static methods and you can call them onto an actual instance the method binding would be done at run-time, not at compile-time. So I was trying to prove you wrong, but you are correct !
    A short summary of what I've learnt so far:
    - interfaces cannot contain static methods
    - abstract classes cannot contain abstract static methods
    - static methods can be overriden, but they are not polymorphic; this means that the compiler decides which method to call at compile-time.
    /* output
    SuperClass.someMethod()
    SubClass.someMethod()
    SuperClass.someMethod()
    SuperClass.someMethod()
    SubClass.someMethod()
    SubClass.someMethod()
    SuperClass.someMethod()
    public class Test {
      public final static void main(String[] args) {
          // output: SuperClass.someMethod()
          new SuperClass().someMethod();
          // output: SubClass.someMethod()
          new SubClass().someMethod();
          // output: 2x SuperClass.someMethod()
          SuperClass someClass1 = new SubClass();
          someClass1.someMethod();
          showSuper(someClass1);
          // output: 2x SubClass.someMethod()
          SubClass someClass2 = new SubClass();
          someClass2.someMethod();
          showSub(someClass2);
          showSuper(someClass2); // SuperClass.someMethod()
      public final static void showSuper(SuperClass c) {
            c.someMethod();
      public final static void showSub(SubClass c) {
            c.someMethod();
    class SuperClass {
      public static void someMethod() {
        System.out.println("SuperClass.someMethod()");
    class SubClass extends SuperClass {
      public static void someMethod() {
        System.out.println("SubClass.someMethod()");

  • Static methods in interface (Yes, once again!)

    I know there has been many questions about why static methods in interfaces are not allowed. I know it is forbidden by the JLS. But does anyone know the exact reason for this? Is it just a design issue or are there technical difficulties with allowing it?
    I can't think of any reason that it should be impossible to allow static methods in interfaces. I don't even think it would be very difficult to do. More importantly, it could probably be done without breaking existing code and existing JVMs (not too sure about the last one).
    So once again: Was the choice of not allowing static methods in interfaces a design choice or a technical choice?

    A better example of what you are suggesting is surely ...
    interface i {
        static void one();
    class a implements i {
        static void one() {
            // do something
    class b implements i {
        static void one() {
            // do something else
    }What would be the point of ever doing this?
    You cant call i.one() as the method doesn't exist without an instance of an implementing class.
    To actually ever be able to use that method you would effectively be casting an INSTANCE of a or b -> i. If you have an instance of an a or b then there is absolutely no reason for that method to be static.
    If you wanted to implement some sort of class (ie static) behaviour then class a could call its own internal static method from within the 'one' method.
    To get right to the point ... there isn't one. If it gets added to the language (and I can't see it in my wildest dreams) then Sun have lost their marbles, surely?
    Unless someone can give a good example of when it might be correct and useful to do so? I can't even see how it would be a convenience as it is completely unusable, no?

  • Too many static methods?

    Hello,
    I am using Oracle 9iAS 9.0.3. My app uses stateless session beans for data access.
    Does anyone have opinions about using static methods in ejb's, specifically there impact on performance under high load? Can a bottleneck be caused by a call to a static method by multiple beans?

    According to EJB Specification you cant declare your business methods static or final.
    --Venky                                                                                                                                                                                                   

  • Can you override a public static method?

    Can you override a public static method?

    A static method belongs to a class, not to a particular object, because you can call them without having an object instantiated. Methods are over rided which are inherited from an object. Since Static methods (although inherited) are not bound to a particular object, are not overridden.
    Though you have a method in the subclass with the same signatures but if you try to call super.____() you will get the error
    non-static variable super cannot be referenced from a static context

  • On static method override-

    just posted an example of over riding a static method . Is this legal? I mock cert exam is saying that static methods can not be overridden, What's the truth?

    As I know static method cannot override by non-static method and vice versa.

  • Static methods  overriding?

    public class SM{
    static void doit(){System.out.println("SM static method");}
    public static void main(String[] args) {
    SM s = new SM();
    s.doit();
    public class SMsub extends SM{
    static void doit(){System.out.println("SM static method this is a subclass");}
    public static void main(String[] args) {
    SMsub s = new SMsub();
    s.doit();
    }

    I think what you have done is perfectly legal, however this is more like "overloading", rather than "overriding" which is the term used in case of polymorphism. Since these methods are static there is no question of polymorphism.

  • Forcing implementation of an inherited static method/field?

    Suppose I have some classes - BlueAction, RedAction etc - extending an abstract base class called BaseAction. I put an abstract method called getColour() in BaseAction, and implement it in all the classes that extend it, so BlueAction returns blue, and so on. Fine.
    I now want to use a class loader to get their colour, but crucially, I don't want to instantiate them, so instance methods are out.
    If you could declare abstract static methods, I'd do that, and be assured that every implementation of BaseAction contained the necessary info. Obviously you can't do this.
    I think I could just put a static method or field in each one, and access that via something like Class.getField("COLOUR")but then there's nothing to force me to put that code into each implementation.
    Is there anything clever I can do?
    Cheers,
    Rob
    Edited by: arewenotmen on Jun 20, 2008 3:51 AM

    baftos wrote:
    I guess it should be possible. Me, I did play with runtime annotations, but I am scared like hell
    of compile time annotation. My reasons, but maybe I am wrong:All good reasons.
    - Modify the build process to use APT instead of javacYou could use in -nocompile mode and add a task to the build script.
    - Writing annotation processors, big overheadI had a play, was not too hard to get started with, but then it started to get wierd and data I expected was not being returned.
    - Using com.sun classes in the processors instead of java/javax classes (subject to change?)Java 6 has the javax.annotations package (also works with javac rather than a different tool), but this seams less powerful than apt.
    A fair few things now mean the "don't touch the com.sun package" rule is being eroded. The httpd is in it as well.

  • Question about static method hiding

    I undestand that a static method may only be hidden in a subclass not overridden. But, I don't understand why an exception type thrown by the subclass method must be a subtype of the exception type thrown by the superclass method. For example,
    public class A {
    public static void foo() throws ExceptionA { }
    public class B extends A {
    public static void foo() throws ExceptionB { }
    gives a compile-time error if ExceptionB is not a subclass of ExceptionA. This makes perfect sense in the non-static case but what does it matter here?
    Thanks,
    Tim

    I don't understand your point. In either case, static
    or non-static, ExceptionB must be the same or a
    subclass of ExceptionA. My question is why this
    matters in the static case.I think it's for binary compatibility, that is, so that you can make changes to Java source files and recompile only the changed files. If you originally did not have the static method in class B, and called B.foo() in another class, the A.foo() method would actually be called at run time. If you then change the B class and add a foo() method, it must be compatible so that the other class's method call is still valid.
    If you're unfamiliar with the concept of binary compatibility, read the chapter in the Java Language Specification. It's a critical but often overlooked aspect of the Java language.

  • HELP: Inheritance question: Overriding a method.

    Hi all.
    I have one class: BatchJob.java which defines the following method:
    (1) protected void handleException(Exception e) {
         setInitError( true );
         printTrace(getClass().getName(), "*** ERROR: " + e.toString());
         createMailError(e);
    Another class JBDCBatchJob.java which extends BatchJob, overrides the previous method like this:
    (2) protected void handleException(SQLException sqlEx) {
         setInitError( true );
         printTrace(getClass().getName(), getSqlQuery(), sqlEx);
         createMailError(getSqlQuery(), sqlEx);
    where getSqlQuery() is a getter for a String member field which contains the last SQL query sent to the JDBC Driver.
    In another class (say SimpleJDBCBatchJob) which extends JDBCBatchJob, I've got the following code fragment:
    try {
    // statements that may throw SQLException or in general Exception
    catch (Exception e) {
    handleException( e );
    I have realized that the method invoked by the JVM when an SQLException is thrown in SimpleJDBCBatchJob is (1), that is, the "handleException" defined in BatchJob and not that in JDBCBatchJob which it should (or at least, that's what I meant). Why is it? Is it possible to accomplish this without having to rename the method (2), I mean, using method overriding? Any idea would be highly appreciated. THANKS in advance.

    You didn't override the method handleException in JBDCBatchJob, you just overloaded it. So int JBDCBatchJob, you actually had two versions of handleException, one that took a parameter of the type Exception, one that took a parameter of the type SQLException.
    So, in SimpleJDBCBatchJob, you need to have a catch block like
    try {
    // statements that may throw SQLException or in general Exception
    catch (SQLException sqle) {
    handleException( sqle );
    catch (Exception e) {
    handleException( e );
    This would call your handleException in BatchJob if a general exception was thrown, and the handleException in JBDCBatchJob if a SQLException was thrown from the try block.
    Hope that makes things clearer for you.
    Alan

  • About static method

    can static methods be overloaded
    can static methods do not have access tp implicit variable calles this

    No overloading concept for static members.
    Try this:
    public class testest extends test2 {
    public static void out() {
    System.out.println (" hello world from testtest ");
    public static void main(String s[]) {
    test2 x = new testest();
    x.out();
    class test2 {
    public static void out() {
    System.out.println (" hello world from test2 ");
    c:\>javac testest.java
    c:\>java testest
    It will give you: hello world from test2.
    The point is: static members are not attached to instance, but to class itself
    So if you do this:
    test2 x = null;
    x.out();
    1. do you think it will compile?
    YES
    2. so what is the result?
    Same, hello world from test2
    3. Why?
    Because you declare x as test2.
    What JVM do is, it will see what class is x, it will find out that x is class of "test2". Then it will execute: test2.out()
    rgds,
    Alex

Maybe you are looking for

  • Bapi to change payment method for vendor invoices using FB02 - VERSION 4.6C

    Hi all, I have a requirment to change payment method from 'A' to 'N' in vendor invoice using tcode FB02 .My system version is 4.6c. I am looking for a BAPI which will serve the purpose. I tried but could not find anyone. Please help me to find the sa

  • Ipod will not restore in disc mode

    I tried the steps listed in ipod support to unfreeze my mini. The icons shift from the apple logo, to file error, to do not disconnect. When I tried to restore it in disc mode, I got a message on my PC that said, "Can't mount ipod." Does this mean th

  • ColdFusion Builder Update 1 issue on Mac

    If you are running ColdFusion Builder Plugin inside Flash Builder 4 on Mac, and you check for updates and install CFB Update 1, make sure you close FB4 before installing the update.  If not, it will blow up your CFB plugin in FB4 and you'll have to r

  • AccessHW is 5 times slower under XP than 2000

    We are using the Port and Memory Utilities for Windows (http://sine.ni.com/apps/we/niepd_web_display.display_epd4?p_guid=B45EACE3DEBD56A4E034080020E74861) to get data from a legacy/custom piece of hardware that is memory mapped on the ISA bus at 0xE0

  • =?ISO-8859-1?Q?=A1=A1=A1NO_A_LA_ELIMINACI=D3N_DE_LAS_?=   =?ISO-8859-1?Q?NEWS!!!?=

    Sabemos de antemano que Adobe no escucha, pero ese no es el punto. El punto es que en http://groups.google.com/group/macromedia.general.espana/topics?lnk=rgh&pli=1 todas estas entradas de protesta quedaran archivadas como antecedentes de lo que le hi