none
Class Loading RRS feed

  • Question

  • Consider the following simple scenario:

    1Module Module1 
    2 
    3    Sub Main() 
    4        C2.i = 9 
    5    End Sub 
    6 
    7End Module 
    8 
    9Public MustInherit Class C1 
    10 
    11    Shared Sub New() 
    12    End Sub 
    13 
    14    Public Shared i As Integer 
    15End Class 
    16 
    17Public Class C2 
    18    Inherits C1 
    19 
    20    Shared Sub New() 
    21    End Sub 
    22End Class 

    Here C2 derives from C1. In main, we access the inherited Member i through C2, i.e. C2.i=9.

    What I knew was that a class is loaded into memory at the first reference to a class (when creating an object, accessing its static member, or during inheritance etc.). And when a class is first loaded, its sole Static constructor fires.

    Here, i is being accessed through C2 (not C1). As the class C2 has been referenced, I thought it would be loaded, and its static constructor would execute. However, to my surprise, only C1's ststic constructor fires, essentially meaning that C2 has NOT been loaded.

    Is that the intended behavior of the runtime, or is it a bug?? Moreover, is there any way to get the C2 class loaded in the above scenario, while accessing the inherited static member i?? Any help would be much appreciated.
    Saturday, November 22, 2008 4:49 AM

Answers

  • Shared field i belongs to C1 class only. It's just a language syntax feature that allows you to reference C1.i as C2.i. Actual piece of code that will appear after compilation is still "C1.i = 9", so class C2 is not referenced.
    • Proposed as answer by PashaPash Saturday, November 22, 2008 2:09 PM
    • Marked as answer by Rahul Singla Sunday, November 23, 2008 2:33 PM
    Saturday, November 22, 2008 2:08 PM

All replies

  • Shared field i belongs to C1 class only. It's just a language syntax feature that allows you to reference C1.i as C2.i. Actual piece of code that will appear after compilation is still "C1.i = 9", so class C2 is not referenced.
    • Proposed as answer by PashaPash Saturday, November 22, 2008 2:09 PM
    • Marked as answer by Rahul Singla Sunday, November 23, 2008 2:33 PM
    Saturday, November 22, 2008 2:08 PM
  • PashaPash said:

    Shared field i belongs to C1 class only. It's just a language syntax feature that allows you to reference C1.i as C2.i. Actual piece of code that will appear after compilation is still "C1.i = 9", so class C2 is not referenced.

    Suppose, there is a third class as following:

    1Public Class C3 
    2    Inherits C1  
    3  
    4    Shared Sub New()  
    5    End Sub  
    6End Class  


    Does, that mean that C3 also has the same copy of i?? If that is correct, isn't it problematic?? C2 & C3 inherit from C1. So, static members of C1 become static members of C2 and C3.
    So, the static members of C1 should be present once in C2 and once in C3. That means while deriving from C1, C2 and C3 get their own copy of i. Is that logically incorrect??
    Sunday, November 23, 2008 5:51 AM
  • Does, that mean that C3 also has the same copy of i?? If that is correct, isn't it problematic?? C2 & C3 inherit from C1. So, static members of C1 become static members of C2 and C3.
    So, the static members of C1 should be present once in C2 and once in C3. That means while deriving from C1, C2 and C3 get their own copy of i. Is that logically incorrect??

    No, C3 and C1 has no their own copies of С1 static fields. Inheritance means that C2 and C3 are extended C1, not an extended copy of C1. Members inherited from C1 remains in C1. You just can put C2 and C3 anywere where C1 is expected, even to get to the C1 static fields.

    Just hover cursor over C2.i =9  to get intellisense tooltip and you will see that there is C1.i there actually.
    Sunday, November 23, 2008 10:37 AM