It all makes no sense at all. Note that the method
doSomethingWithVirtualMethodResult
, despite its name, has nothing to do with
VirtualMethod
or its result. It simply can be called with any string.
"Calling statically" sounds like absurd, but in fact, it's simply unclear what would you mean by that. No
static members are involved, so what you want has nothing to do with the only well-defined concept related to
static in Java technology: static members vs
instance members. All your members are instance members, that is, the explicit "this" parameter is passed to
instance methods, to reference the particular instance of the class. Essentially, there is no such thing as a call without such instance.
So, you need a solution, right? The solution is: learn how OOP works and pose your problems correctly. Your problem is not solvable or unsolvable, it simply makes no sense as it is put. For now, I'll explain how things work in your case. Your
VirtualMethod
is virtual and overridden. There is no such thing as "called with both something and something else", because it is not called with any parameter. There is no anything "to call it with". You only have two different return objects. But there is a parameter called explicitly, "this". Say, you create two instances, one of one type and another of another type (important
runtime type, the implementation of the method is dynamically chosen based on the reference "this". This is a heart of OOP, its central mechanism. All calls are, by definition, are based on the reference "this", in the case of a virtual method, on its runtime type. Please see:
https://en.wikipedia.org/wiki/Dynamic_dispatch[
^].
You may ask: what to do? Nothing. Just learn how things work. Your other problem is the question you ask. You are not asking about anything material, something which should really happen in behavior. Then we could help you immediately. But you are asking in terms of some non-existing concept which only exist in your imagination, such as "calling statically", which is, essentially, is not something which happens in observable world. Perhaps you need to learn how to formulate and understand your ultimate goals.
—SA