<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    gembin

    OSGi, Eclipse Equinox, ECF, Virgo, Gemini, Apache Felix, Karaf, Aires, Camel, Eclipse RCP

    HBase, Hadoop, ZooKeeper, Cassandra

    Flex4, AS3, Swiz framework, GraniteDS, BlazeDS etc.

    There is nothing that software can't fix. Unfortunately, there is also nothing that software can't completely fuck up. That gap is called talent.

    About Me

     

    Overriding Vs Hiding

    http://yuzan.me/

    Can I override a static method?

    Many people have heard that you can't override a static method. This is true - you can't. However it is possible to write code like this:


    class Foo {
        public static void method() {
            System.out.println("in Foo");
        }
    }

    class Bar extends Foo {
        public static void method() {
            System.out.println("in Bar");
        }
    }

    This compiles and runs just fine. Isn't it an example of a static method overriding another static method? The answer is no - it's an example of a static method hiding another static method. If you try to override a static method, the compiler doesn't actually stop you - it just doesn't do what you think it does.

    So what's the difference?

    Briefly, when you override a method, you still get the benefits of run-time polymorphism, and when you hide, you don't. So what does that mean? Take a look at this code:


    class Foo {
        public static void classMethod() {
            System.out.println("classMethod() in Foo");
        }

        public void instanceMethod() {
            System.out.println("instanceMethod() in Foo");
        }
    }

    class Bar extends Foo {
        public static void classMethod() {
            System.out.println("classMethod() in Bar");
        }

        public void instanceMethod() {
            System.out.println("instanceMethod() in Bar");
        }
    }
     
    class Test {
        public static void main(String[] args) {
            Foo f = new Bar();
            f.instanceMethod();
            f.classMethod();
        }
    }

    If you run this, the output is

    instanceMethod() in Bar classMethod() in Foo

    Why do we get instanceMethod from Bar, but classMethod() from Foo? Aren't we using the same instance f to access both of these? Yes we are - but since one is overriding and the other is hiding, we see different behavior.

    Since instanceMethod() is (drum roll please...) an instance method, in which Bar overrides the method from Foo, at run time the JVM uses the actual class of the instance f to determine which method to run. Although f was declared as a Foo, the actual instance we created was a new Bar(). So at runtime, the JVM finds that f is a Bar instance, and so it calls instanceMethod() in Bar rather than the one in Foo. That's how Java normally works for instance methods.

    With classMethod() though. since (ahem) it's a class method, the compiler and JVM don't expect to need an actual instance to invoke the method. And even if you provide one (which we did: the instance referred to by f) the JVM will never look at it. The compiler will only look at the declared type of the reference, and use that declared type to determine, at compile time, which method to call. Since f is declared as type Foo, the compiler looks at f.classMethod() and decides it means Foo.classMethod. It doesn't matter that the instance reffered to by f is actually a Bar - for static methods, the compiler only uses the declared type of the reference. That's what we mean when we say a static method does not have run-time polymorphism.

    Because instance methods and class methods have this important difference in behavior, we use different terms - "overriding" for instance methods and "hiding" for class methods - to distinguish between the two cases. And when we say you can't override a static method, what that means is that even if you write code that looks like it's overriding a static method (like the first Foo and Bar at the top of this page) - it won't behave like an overridden method.

    So what about accessing a static method using an instance?

    It's possible in Java to write something like:

       f.classMethod();

    where f is an instance of some class, and classMethod() is a class method (i.e. a static method) of that class. This is legal, but it's a bad idea because it creates confusion. The actual instance f is not really important here. Only the declared type of f matters. That is, what class is f declared to be? Since classMethod() is static, the class of f (as determined by the compiler at compile time) is all we need.

    Rather than writing:

        f.classMethod();
    It would be better coding style to write either:
        Foo.classMethod();
    or
        Bar.classMethod(); 
    That way, it is crystal clear which class method you would like to call. It is also clear that the method you are calling is indeed a class method.

    Barring that, you could always come up with this monstrosity:

        f.getClass().getMethod("classMethod", new Class[]).invoke(null, new Object[]);

    But all this could be avoided by simply not trying to override your static (class) methods. :-)

    Why does the compiler sometimes talk about overriding static methods?

    Sometimes you will see error messages from the compiler that talk about overriding static methods. Apparently, whoever writes these particular messages has not read the Java Language Specification and does not know the difference between overriding and hiding. So they use incorrect and misleading terminology. Just ignore it. The Java Language Specification is very clear about the difference between overriding and hiding, even if the compiler messages are not. Just pretend that the compiler said "hide" rather than "override"..

    ref: http://www.coderanch.com/how-to/java/OverridingVsHiding

    posted on 2014-05-29 15:52 gembin 閱讀(612) 評論(0)  編輯  收藏 所屬分類: JavaSE

    導航

    統計

    常用鏈接

    留言簿(6)

    隨筆分類(440)

    隨筆檔案(378)

    文章檔案(6)

    新聞檔案(1)

    相冊

    收藏夾(9)

    Adobe

    Android

    AS3

    Blog-Links

    Build

    Design Pattern

    Eclipse

    Favorite Links

    Flickr

    Game Dev

    HBase

    Identity Management

    IT resources

    JEE

    Language

    OpenID

    OSGi

    SOA

    Version Control

    最新隨筆

    搜索

    積分與排名

    最新評論

    閱讀排行榜

    評論排行榜

    free counters
    主站蜘蛛池模板: 亚洲综合无码一区二区三区| 一级**爱片免费视频| 亚洲夜夜欢A∨一区二区三区| 亚洲黄色免费观看| 中文成人久久久久影院免费观看| 综合偷自拍亚洲乱中文字幕 | 免费无码一区二区| 亚洲国产精品免费观看| 久久久久亚洲精品无码蜜桃| 中文亚洲AV片不卡在线观看| 国产免费牲交视频| 女人18毛片特级一级免费视频 | 91在线精品亚洲一区二区| 亚洲乱码精品久久久久..| 亚洲Av无码乱码在线znlu| 免费看大黄高清网站视频在线| 在线永久免费的视频草莓| 曰批全过程免费视频网址| 一区二区三区观看免费中文视频在线播放 | 亚洲av无码国产综合专区| 亚洲综合区图片小说区| 亚洲精品高清久久| 亚洲国产二区三区久久| 亚洲AV福利天堂一区二区三| 亚洲成色在线综合网站| 亚洲AV无码成人网站久久精品大 | 免费看美女午夜大片| 国产亚洲男人的天堂在线观看| 亚洲AV无码一区二区乱子仑| 亚洲熟伦熟女专区hd高清| 国产成人精品日本亚洲直接| 亚洲福利一区二区| 亚洲人成影院午夜网站| 色噜噜亚洲男人的天堂| 亚洲日本在线电影| 校园亚洲春色另类小说合集| 免费无遮挡无码视频在线观看| 特级aaaaaaaaa毛片免费视频| 九九免费久久这里有精品23| sihu国产精品永久免费| 全黄大全大色全免费大片|