This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method related doubts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static method related doubts" Watch "Static method related doubts" New topic
Author

Static method related doubts

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583



  • I can use startCar() in main method ,is it mean we can inherit static method but can't override.Am i right ?
  • I don't like this code really , super.startCar() , i know that super keyword is used to call overridden method version of superclass but as universal truth we can't override static method then why it is working here, what is the wrong with java ?


  • Tell the difficulties that i am difficult.
    Ankit Garg
    Sheriff

    Joined: Aug 03, 2008
    Posts: 9291
        
      17

    There is nothing wrong with Java

    You can call a static method on an instance of a class although its not recommended. Due to this reason you can call a static method on this or super references. You are right that static methods are inherited but they can't be overridden...


    SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Ankit Garg wrote:There is nothing wrong with Java

    You can call a static method on an instance of a class although its not recommended. Due to this reason you can call a static method on this or super references. You are right that static methods are inherited but they can't be overridden...


    Oh come on ,java gives us lecture that make your classes cohesive then what is java doing ?, super is used to call overridden method, where as in context of static method there is no overriding then why anyone like to use super here ? why don't we use directly startCar() or this.startCar() or obj.startCar().It doesn't give fake impression of overriding in static context ? [super.startCar()]
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4338
        
        7

    You seem to be blaming Java for the fact that you aren't using the correct approach, which is Car.startCar().

    It's true that it's a bit unfortunate that Java allows you to call static methods on an object reference, when you should never do that. If they were designing this feature now I suspect it would be changed, but this dates back to Java 1.0 and it would be too big an impact to change it now (because of the number of people who have already written code doing it wrong ).
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Matthew Brown wrote:You seem to be blaming Java for the fact that you aren't using the correct approach, which is Car.startCar().

    It's true that it's a bit unfortunate that Java allows you to call static methods on an object reference, when you should never do that. If they were designing this feature now I suspect it would be changed, but this dates back to Java 1.0 and it would be too big an impact to change it now (because of the number of people who have already written code doing it wrong ).



    Thanks Matthew, Can you tell me mystery of super keyword.
    gurpeet singh
    Ranch Hand

    Joined: Apr 04, 2012
    Posts: 924
        
        1

    that's a bit unfortunate that java allows you to call static methods on instance variables(including this and super reference). also static methods cannot be overridden though they are inherited. Consider the following code listings.




    the above code will produce following output :

    The class method in Animal.
    The instance method in Cat.
    The class method in Animal.
    The class method in Cat

    the important thing about this is the line labelled as label a and label b. myAnimal.testClassMethod() will invoke the static method of animal class because myAnimal is of type Animal(although it is referring to cat object). internally it gets replace with Animal.testClassMethod(). Similarly mycat.testClassMethod() gets replaced with Cat.testClassMethod(). Please guys correct me whereever i'm wrong.
    raj malhotra
    Ranch Hand

    Joined: Feb 22, 2007
    Posts: 285
    i know that super keyword is used to call overridden method version of superclass but as universal truth we can't override static method then why it is working here, what is the wrong with java ?


    It is not possible to override static method. But it is possible to hide the static method by defining another method with the same signature in the subclass. So actually we can call the supper class static method by invoking SUPER_CLASS.staticMethod() or superClassINstance.staticMethod() or super.staticMethod() in subclass.

    Even if you write superClassINstance.staticMethod() , compiler will change it to SUPER_CLASS.staticMethod().

    Thanks,
    Raj
    Arunkumar Chinnadurai
    Ranch Hand

    Joined: Dec 15, 2011
    Posts: 56

    saloni jhanwar wrote:
    Ankit Garg wrote:There is nothing wrong with Java

    You can call a static method on an instance of a class although its not recommended. Due to this reason you can call a static method on this or super references. You are right that static methods are inherited but they can't be overridden...


    Oh come on ,java gives us lecture that make your classes cohesive then what is java doing ?, super is used to call overridden method, where as in context of static method there is no overriding then why anyone like to use super here ? why don't we use directly startCar() or this.startCar() or obj.startCar().It doesn't give fake impression of overriding in static context ? [super.startCar()]


    ok i have one doubt on this thread., without static keyword what will happen in java..?
    your question is good but you should analyse all the face ok.

    I just want to access a method in class one . so i created that as static . i think you know very well about static , before instance creation itself static block will load,
    or if you want to override mean please be that method as normal only.
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4338
        
        7

    saloni jhanwar wrote:Thanks Matthew, Can you tell me mystery of super keyword.

    Well, as I said, it's (unfortunately) legal to call a static method on a reference of that class. Within the Limo class, super is effectively a reference to the Car class. So allowing the calling of a static method on it is just being consistent.
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    raj malhotra wrote:
    i know that super keyword is used to call overridden method version of superclass but as universal truth we can't override static method then why it is working here, what is the wrong with java ?


    It is not possible to override static method. But it is possible to hide the static method by defining another method with the same signature in the subclass. So actually we can call the supper class static method by invoking SUPER_CLASS.staticMethod() or superClassINstance.staticMethod() or super.staticMethod() in subclass.

    Even if you write superClassINstance.staticMethod() , compiler will change it to SUPER_CLASS.staticMethod().

    Thanks,
    Raj



    That is called redefining method not hiding.
    Matthew Brown
    Bartender

    Joined: Apr 06, 2010
    Posts: 4338
        
        7

    saloni jhanwar wrote:That is called redefining method not hiding.

    No, it's definitely hiding.

    The super-class version can still be called, using the class name. But calling it just by name in the subclass will call the subclass version, as the superclass version is now "hidden".
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Arunkumar Chinnadurai wrote:
    saloni jhanwar wrote:
    Ankit Garg wrote:There is nothing wrong with Java

    You can call a static method on an instance of a class although its not recommended. Due to this reason you can call a static method on this or super references. You are right that static methods are inherited but they can't be overridden...


    Oh come on ,java gives us lecture that make your classes cohesive then what is java doing ?, super is used to call overridden method, where as in context of static method there is no overriding then why anyone like to use super here ? why don't we use directly startCar() or this.startCar() or obj.startCar().It doesn't give fake impression of overriding in static context ? [super.startCar()]


    ok i have one doubt on this thread., without static keyword what will happen in java..?
    your question is good but you should analyse all the face ok.

    I just want to access a method in class one . so i created that as static . i think you know very well about static , before instance creation itself static block will load,
    or if you want to override mean please be that method as normal only.


    I don't know your answer is about what anyway thanks.
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Matthew Brown wrote:
    saloni jhanwar wrote:Thanks Matthew, Can you tell me mystery of super keyword.

    Well, as I said, it's (unfortunately) legal to call a static method on a reference of that class. Within the Limo class, super is effectively a reference to the Car class. So allowing the calling of a static method on it is just being consistent.


    Thanks Matthew
    saloni jhanwar
    Ranch Hand

    Joined: Feb 09, 2012
    Posts: 583

    Matthew Brown wrote:
    saloni jhanwar wrote:That is called redefining method not hiding.

    No, it's definitely hiding.

    The super-class version can still be called, using the class name. But calling it just by name in the subclass will call the subclass version, as the superclass version is now "hidden".


    Thanks Matthew , in K&B for this "redefining" word is used but now i got that .
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Static method related doubts
     
    Similar Threads
    Encapsulation, coupling and cohesion
    DOUBT on DOWNCASTING
    ArrayStoreException - Inquition doubt
    simple overriding of static method
    Static methods cannot be overridden ?