Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused about static and instance methods with same name

 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


doubt:

what is happening ?
either the static method is overloaded as non static method
or
compiler treats it as a different method though it has a same name as the static method?
 
Maneesh Godbole
Saloon Keeper
Posts: 11338
16
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What happens when you try out the following:
 
Les Morgan
Rancher
Posts: 711
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can overload static methods, you just cannot override them. So try to extend A and override one of the static methods and see what happens.
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:What happens when you try out the following:


Respective methods are called.

i was right on this.wasn't i?

compiler treats it as a different method though it has a same name as the static method?




 
Les Morgan
Rancher
Posts: 711
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:
i was right on this.wasn't i?


That overloading is not a problem, but the following seems to be a problem:

None of those are static Classes, but the static method is supposed to resolve back to the Class, not the Instance, but class B does infact do an Override of F1() from A, but if I try to use the Annotation @Override, my NetBeans says there is no method to override, but as displayed in the call from JStaticJunk there is an F1(int) inherited from A, so where did F1() go? In all of it the Class variable works properly.
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:
s sivaraman wrote:
i was right on this.wasn't i?


That overloading is not a problem, but the following seems to be a problem:

None of those are static Classes, but the static method is supposed to resolve back to the Class, not the Instance, but class B does infact do an Override of F1() from A, but if I try to use the Annotation @Override, my NetBeans says there is no method to override, but as displayed in the call from JStaticJunk there is an F1(int) inherited from A, so where did F1() go? In all of it the Class variable works properly.



would it compile? because you cannot access a static method by an instance reference.
 
Les Morgan
Rancher
Posts: 711
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:
would it compile? because you cannot access a static method by an instance reference.


Not only does it compile without any error or warnings, here is the output:

run:
This is F1(int) 11
This is F1()11
F1() 11
This is F1(int) 12
This is F1()12
BUILD SUCCESSFUL (total time: 0 seconds)

It's all members of the non static class A and the non static class B, so to get a reference you have to instantiate A or B.

Notice that class variable behaves just like it is supposed to do.
 
s sivaraman
Ranch Hand
Posts: 59
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



if you'd used this,instead of



i rekcon you'd get the same output

run:
This is F1(int) 11
This is F1()11
F1() 11
This is F1(int) 12
This is F1()12
BUILD SUCCESSFUL (total time: 0 seconds)



Notice that class variable behaves just like it is supposed to do.


Then why using the code syntax that affects the readability?
 
Les Morgan
Rancher
Posts: 711
17
C++ Java MySQL Database Netbeans IDE Oracle Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:
Then why using the code syntax that affects the readability?


Stefon Evan answered why the strangeness in this thread: http://www.coderanch.com/t/657279/java/java/static-method-strange-behavior

Basically some misconception on my behalf and further clarification.
 
Sachin Tripathi
Ranch Hand
Posts: 368
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:
would it compile? because you cannot access a static method by an instance reference.


yes,it will compile,it is not recommended to use instance reference to access static method or static fields(it is not a good practice) but you can surely do it.

Most important point:
Static fields and variables,will pay no attention to the instances,even though after being accessed by instance reference.The only thing to which they pay the attention is the class of reference variable
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:what is happening ?
either the static method is overloaded as non static method
or
compiler treats it as a different method though it has a same name as the static method?

You have two overloaded static (class) methods F1: one method has no parameters and another has an int parameter. And class A also has an instance method with exactly the same name as the static (class) methods (F1) and a double parameter. So the parameter of the instance method is different from the paramters of the two static (class) methods with exactly the same name. And that's really very important! Because you can not have a static (class) method and an instance method with exactly the same method signature (name and parameter list). This code snippet won't compileThe reason why is pretty obvious: using an instance of a class you can invoke both an instance method and a static (class) method, so with same method signatures it's impossible to know for the compiler if you wanted to invoke the instance method or static (class) one. As in this code snippetTherefore you'll get a compiler error if an instance method and a static (class) method have exactly the same method signature.

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
s sivaraman wrote:because you cannot access a static method by an instance reference.

That's absolutely incorrect! Using a reference variable you can definitely invoke a static method. It's not considered to be a good practice (you should use the class name), because it decreases the readibility of your code. But it's allowed, you will not get a compiler error at all! Example:Output: in method1

And even if you would use this main method, you'll get in method1 as output and thus not a NullPointerException

Hope it helps!
Kind regards,
Roel
 
Roel De Nijs
Sheriff
Posts: 10662
144
AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Les Morgan wrote:but class B does infact do an Override of F1() from A, but if I try to use the Annotation @Override, my NetBeans says there is no method to override, but as displayed in the call from JStaticJunk there is an F1(int) inherited from A, so where did F1() go? In all of it the Class variable works properly.

And NetBeans is absolutely correct! Static (class) methods are never overridden. Never! Repeat after me: static (class) methods are never overridden Only instance methods can be overridden (if the instance method is inherited). So that means, a private instance method can never be overridden as well.

But static methods can be "inherited" as well. And if you declare a static method in a subclass with exactly the same method signature as in the parent class, then the static method in the parent class is replaced with the one in the subclass. But (and now it gets really interesting) the same rules as with overriding instance methods apply:
  • the access level cannot be more restrictive
  • the return type should be the same or a subtype of the return type
  • the method can only throw narrower or fewer checked exceptions
  • a final method can't be replaced
  • So the following code snippets do not compile. Do you know why?But this code snippet compiles successfully

    Hope it helps!
    Kind regards,
    Roel

    PS. Did you intentionally add the line new JStaticJunk(); to the main method? Because it's not required to create a new instance of the JStaticJunk class to run the application.
     
    Roel De Nijs
    Sheriff
    Posts: 10662
    144
    AngularJS Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Now let's see what happens if all static occurences (except for the main method) in the JStaticJunk class are removed. What's the output of this code snippet?

    Hope it helps!
    Kind regards,
    Roel

    PS. If you look carefully, you'll see I changed the first line in the main method as well. If you would make the same change in the JStaticJunk class, you would still get the exact same output as mentioned in one of the previous posts. So for the original code snippet, it makes no difference at all. But for this one, it definitely will
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic