Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 3548

Re: Determine class name used for calling a static method

$
0
0

Hi Horst.


Quote from your post:

I don't see point (5) above as an exception to (4) but as a consequence because naming the class in the method call is not the same as addressing a component.


Just to check if we are using the same terms in the same sense: When in a language we refer to some data or code object (including class components), we are addressing some entity (the object) in a particular memory location. It's clear that the adressing occurs as a consequence of the reference, like when naming a static class component. In ABAP, the components of a class:attributes and methods, are examples of those addressed objects.


Looking clearly, the note (5) is not an exception of (4) but a contradiction. The note (5) says that if we refer to "zcl_sub=>something()", where something is a static component inherited from zcl_super(and not redefined) the compiler will interpret this as if we would be referring to the "zcl_super=>something()"object, in the sense it will address that object.


As we are talking about static components, that compiling simplification seems harmless... until we realize that as a consequence of this, compiler ends up calling only the constructor of zcl_superand not the one of zcl_sub. However, when we as developers, referred to "zcl_sub=>something()", we didn't mean to refer to "zcl_super=>something()",right?Is the compiler the one that makes that switch, and in a so strictly way that it doesn't call the expected static zcl_sub=>constructor but only the zcl_super one.


Compare the above facts with the point (4), composed by the points (1) + (3), also in ABAP documentation. The point (1) says"The static constructor is called automatically ... before the class is first accessed. An access to the class is ... the addressing of a static component using the class component selector". The tricky part here is: it is not possible to address a subclass inherited component! you can refer to it in your code "zcl_sub=>something" but for the compiler considerations, you are addressing to"zcl_super=>something".This is a proposition of the form "P then Q" (addressing a static component thenthe static constructor is called automatically) where P can never be true (in our case of inherited static components), so the proposition in red is always true!!


Nonetheless, the part (3) in (4) says "When a subclass is first accessed... the static constructor of the superclass is executed followed by those of all subsequent subclasses up to the subclass in question...". Clearly what happens contradict this.

Regarding a static constructor, there are no objects and its task is to set the static attributes before they are needed the first time. And that is fullfilled in ABAP. Prove me wrong.


Horst, of course that was proven wrong. In the example of Mike Pokraka, the call to the method zcl_sub=>say_hello() requires the previous setting of the hello attribute with the value "World" by the constructor, right when the zcl_sub class is referred for the very first time. However, for all we have seen above, the compiler, doesn't do that and executes only the zcl_super constructor, completely disregarding the semantic meaning of the zcl_sub static constructor. That's why the zcl_sub=>say_hello() unexpectedly writes "Hello".

Or even better, follow thre programming guideline

 

Whaat??!! The Programming Guidelines are in public domain ? I demand the refund for the book "Official ABAP ™ Programming Guidelines" I bought from SAP Press in 2013!!


Now I understand why that guidelines recommend so adamantly to avoid the use of static classes (those with only static components). Conceptually I agree with that way of thinking, for all the good reasons exposed there. And now, even more because the issue we are dealing here.


However, the described situation in ABAP affects not only static classes, but those with just few static members. For example, I have a problem dealing with a hierarchy of exception classes whose constructor, as you know, are autogenerated by the Netweaver and moreover can not be edited! This means, the only way to construct them, without the regular constructors (this is for very good reasons, beyond the scope of this thread) is with static components. But this is minefield if I mix regular OOP factory designs with this peculiarity in ABAP.


You should warn about this with red big size letters in the documentation, otherwise many will become crazy trying to chase a bug in their code, when it is a language feature.


Interestingly, in the Scala language there are not static class components, they use instead "the companion object", decoupling completely the static components from the really OOP ones. This way they can define any particular behavior for those companion objects without clashing well known OOP behavior expected by the users as it is happening here.


Best regards.


OdL


Viewing all articles
Browse latest Browse all 3548

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>