This is the mail archive of the java-patches@gcc.gnu.org mailing list for the Java project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Socket timeout patch


A followup to my last response (is that a good or a bad sign?)

Eric Blake wrote:
> 
> You may also be interested in this comment from JVMS 2.17.6 (step 4 in
> initializing instances):

This quote is also in JLS 12.5, step 4

> 
> "(In some early implementations, the compiler incorrectly omitted the
> code to initialize a field if the field initializer expression was a
> constant expression whose value was equal to the default initialization
> value for its type. This was a bug.)"

Here's an example where it makes a difference:
class Super
{
  Super() { m(); }
  void m() {}
}
class Sub extends Super
{
  int i = 0;
  void m() { i = 1; }
  public static void main(String[] args)
  {
    System.out.println(new Sub().i);
  }
}

The instance initializer i = 0 is executed after the superconstructor
has called m() polymorphically; the program must output 0, not 1.

> 
> Of course, that statement applies to bytecode emission; when compiling
> to native code, I see no problem in omitting initializations if it is to
> the default value. And the above comment only applied to instance
> fields; to my knowledge Sun has never forbidden omitting initializers
> for class fields.

Anything that can be done polymorphically with instance methods can be
imitated with class initializers; therefore I retract the above claim,
and say instead that a compiler may NOT omit default initializations,
not even for static fields:

class Super
{
  static {
    Sub.i = 1;
  }
}
class Sub extends Super
{
  static int i = 0;
  public static void main(String[] args)
  {
    System.out.println(i);
  }
}

If the initializer execution is omitted, the above example will
mistakenly print 1 instead of 0.

Therefore, if you as the programmer know that the initial value of a
variable is satisfactory and will not be assigned by code in the
superclass, don't initialize it!  You will cut down on the amount of
emitted code.

-- 
This signature intentionally left boring.

Eric Blake             ebb9@email.byu.edu
  BYU student, free software programmer


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]