gusl: (Default)
[personal profile] gusl
I just ran into problems because of Java's static (non pun intended) typing. Say we have 3 classes: A extends B extends C

We have an object named instance whose class is A.

When instance.inputMatcher(x,y) gets called, we would expect the Java dispatcher to look for the implementation of inputMatcher in this order:
* inside A
* inside B
* inside C

This is indeed what happens if the inputMatcher methods are non-static.

If they are static, however, Java will insist on calling the inputMatcher() from C, because that is the declared type of instance.

Therefore, we need to down-cast. For example:
if (matcherInstance.getClass().equals("A"))
    result = ((A) instance).inputMatcher(x,y);


But this is bad, since it would require me to create a case for each and every subclass of C, in which I down-cast instance to that type.

The code I really want is something like:
result = downcast(instance, instance.getClass()).inputMatcher(a,b);

(no subject)

Date: 2006-10-12 05:41 am (UTC)
From: [identity profile] roseandsigil.livejournal.com
What do static methods mean? Why don't you just make that method nonstatic? Then the method will be called according the runtime class of the object.

(no subject)

Date: 2006-10-12 12:54 pm (UTC)
From: [personal profile] chrisamaphone
I think a static method is one you can call without creating an instance of the class it's in?

(no subject)

Date: 2006-10-12 06:32 pm (UTC)
From: [identity profile] ekorber.livejournal.com
This is correct. Static methods cannot depend on the value of instance variables, since they are not being called on an instance.

The keyword "static" is highly misleading as what it does to a method has nothing whatsoever to do with what any programmer or normal person has as their definitions of "static".

(no subject)

Date: 2006-10-12 05:42 am (UTC)
From: [identity profile] jcreed.livejournal.com
I'm confused why it's a static method if you want to do dynamic dispatch on the tag of the object.

(no subject)

Date: 2006-10-12 05:56 am (UTC)
From: [identity profile] bhudson.livejournal.com
Java is confused as to the meaning of "static."

Static means two things: (1) doesn't pass the 'this' pointer in, so you have no access to the instance's guts; (2) doesn't do dynamic dispatch.

Those two are separable concepts -- (1) means you save pushing an unused argument on the stack, so performance hogs go for it (which could conceivably matter in a recursive function). You could still do dynamic dispatch to figure out what function to call first.

(no subject)

Date: 2006-10-12 06:01 am (UTC)
From: [identity profile] bhudson.livejournal.com
Oh, another problem is the syntax. You have a variable 'Cow bessie' that points to an instance of a Jersey. You call "bessie.milk()" on it. If it turns out that some fucktard made 'milk' static while you weren't looking (presumably to save that pointer's overhead), suddenly you're calling Cow.milk() rather than Jersey.milk().

This is "convenient" in terrible ways, like unityped languages.
(deleted comment)

(no subject)

Date: 2006-10-12 05:40 pm (UTC)
From: [identity profile] bhudson.livejournal.com
I think I've forgotten what 'extends' means. Isn't it the keyword for subclassing? In what way is that evil?

(I mean, Java is of course evil, but that's another fight I don't want to get in to today.)

February 2020

S M T W T F S
      1
2345678
9101112131415
16171819202122
23242526272829

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags