> Initialization of Fields in Interfaces ~ Online tutorial

Initialization of Fields in Interfaces

Initialization of Fields in Interfaces
Every field in the body of an interface must have an initialization expression,
which need not be a constant expression. The variable initializer is evaluated and
the assignment performed exactly once, when the interface is initialized
A compile-time error occurs if an initialization expression for an interface
field contains a reference by simple name to the same field or to another field
whose declaration occurs textually later in the same interface.
Thus:

interface Test
{
float f = j;
int j = 1;
int k = k+1;
}

  • two compile-time errors, because j is referred to in the initialization of
    before j is declared and because the Initialization
    of k refers to k itself.
  • One subtlety here is that, at run time, fields that are initialized with compiletime constant values are initialized first. This applies also to static final fields in classes
  • This means, in particular, that these fields will never be
    observed to have their default initial values , even by devious programs.
    and for more discussion.
  • If the keyword this ;or the keyword super occurs in an initialization expression for a field of an interface, then unless the occurrence is within the body of an anonymous class a compile-time error occurs.

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: