Win a copy of Rust Web Development this week in the Other Languages forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

instance method/static context/dynamic binding

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In one Java textbook (Liang) is the following quote: "When invoking an instance method from a reference variable the class of the actual object referenced by the variable decides which implementation of the method is used at runtime. When accessing a field or static method, the declared type of the reference variable decides which method is used at compilation time."
Is it also true that when an instance method in an object created in a static context is invoked from that static context it behaves like a static method in terms of dynamic binding?
I have a situation that appears to demonstrate that that is the case. It's a little lengthy to post.

Thanks.
Dennis
 
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
No, you cannot invoke a static method from within a non-static context, unless you qualify the call with the class name. This is because an implicit 'this' reference is placed before the method call, and we all know (I hope) that there is no such concept as the 'this' reference in a static context.

If you post a code sample, I (or someone else) may be able to point out where you have misunderstood.
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Correction:
 
Dennis Diment
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[/I]"No, you cannot invoke a static method from within a non-static context"[/I]
That's not what I asked. Maybe I didn't express my problem clearly enough. My question pertains to a different scenario: when an instance method in an object created in a static context is invoked from that static context does it behave like a static method in terms of dynamic binding?
In the first example below, a1 and a2 are references of type Object. In the second example, they are of type A. Other than that, the programs are identical. They produce different results because different implementations of equals() are being invoked. This shouldn't be happening, according to the rule (more fully) stated in my original post: "when invoking an instance method . . . the class of the actual object . . . decides which implementation of the method is used . . . When accessing a field or static method, the declared type of the reference variable decides . . ." Why, then, in the first example, is the reference type of the variables deciding which implementation of this instance method to use?
I'm wondering (guessing) if it's because the object is created and used in a static context? I'm shooting in the dark here (the last resort of the confused).

Your statement (quoted above) leaves me a little confused. In the following example I'm calling a static method from two different non-static contexts, both without a class-name qualifier.

I know the class name qualifier can be used without having to create an object, but the static member can actually be used from a non-static context without it.

Thanks.
Dennis Diment
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I wonder what I was thinking (drinking?) that day.
I take it all back - my deepest apologies.
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The static method can indeed be used as if it were a non-static method, but it still can access only static data.
You cannot create instance variables in a static method, only method-local variables.
These are scoped to the method only (or more localised to blocks within the method) and will go out of scope at the latest when the current method invocation exits.
 
Dennis Diment
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"You cannot create instance variables in a static method, only method-local variables."
main() is a static method. In each of the above examples objects are instantiated in main(). Object references there certainly are method-local, but they are objects all the same.
"These are scoped to the method only (or more localised to blocks within the method) and will go out of scope at the latest when the current method invocation exits."
Aren't instance variables method-local in non-static methods as well? Aren't all instance variables method-local/block-local?
I am new at this. Am I misunderstanding something fundamental here?
The point of my original post is getting somewhat lost. My question is about the behavior of dynamic binding.
To reiterate: if it's true that, in the case of an instance method the actual object determines the implementation regardless of where the reference variable lies in the inheritance hierarchy, and in the case of a static method the reference variable determines the implementation, why do I get the results that I showed in my original post? (There's no sense in duplicating the code here.) equals() is a non-static instance method in both cases (in both class Object and class A). Yet, the reference variable is doing the choosing (determining the implementation).
I'm wondering if it's because the context it's all being done from is static. Actually, this solution doesn't seem right to me. I just don't know what else to think.
Thanks again.
Dennis Diment
ps - If I don't figure this out soon, I'm going to give up on programming and learn how to shoe horses.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic