Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Static method related doubts

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


  • 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 ?
  •  
    Ankit Garg
    Sheriff
    Posts: 9509
    22
    Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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...
     
    saloni jhanwar
    Ranch Hand
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4565
    8
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 924
    1
    Fedora Java Netbeans IDE
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 288
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 56
    Android MyEclipse IDE Windows XP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4565
    8
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 4565
    8
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 583
    Firefox Browser Notepad Windows
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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 .
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic