• Post Reply Bookmark Topic Watch Topic
  • New Topic

when A class or interface will be initialized  RSS feed

 
Akshay Rawal
Ranch Hand
Posts: 99
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ranchers!!

..ACCORDING TO ME OUTPUT SHOULD BE "QBANK" In Sub...BECAUSE sub default constructor will call super() constructor..below is the definition in jls which i am unable to understand ..can someone please explain me in easy form ..thanks

A class or interface type T will be initialized at its first active use, which occurs if:
T is a class and a method actually declared in T (rather than inherited from a
superclass) is invoked.
T is a class and a constructor for class T is invoked, or T1 is an array with element
type T, and an array of type T1 is created.
A non-constant field declared in T (rather than inherited from a superclass or
superinterface) is used or assigned. A constant field is one that is (explicitly or
implicitly) both final and static, and that is initialized with the value of a compile-time
constant expression . Java specifies that a reference to a constant field must be
resolved at compile time to a copy of the compile-time constant value, so uses of such
a field are never active uses.
All other uses of a type are passive
A reference to a field is an active use of only the class or interface that actually
declares it, even though it might be referred to through the name of a subclass, a
subinterface, or a class that implements an interface.
 
Joanne Neal
Rancher
Posts: 3742
16
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Akshay Rawal wrote:sub default constructor will call super() constructor

Constructors are only called when you create an instance of a class. You are not creating any instances in your code.

This is the relevant part
Java specifies that a reference to a constant field must be resolved at compile time to a copy of the compile-time constant value, so uses of such a field are never active uses.

Thus your test class will be compiled as though it were

So the Sub class is never loaded, so its static block is never called.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!