Re: Statements before super()

Discussion in 'Java' started by Roedy Green, Feb 10, 2010.

  1. Roedy Green

    Roedy Green Guest

    On Tue, 09 Feb 2010 16:22:35 +0000, Steven Simpson <>
    wrote, quoted or indirectly quoted someone who said :

    >Could that restriction not be loosened in a compiler-verifiable way,
    >i.e. check that no statement prior to super() uses 'this' (explicitly or
    >implicitly)?


    It would have to chase any method calls recursively. They might
    dynamically load code. I don't think you can tell in general.

    Perhaps you could allow an expression involving only literals, static
    finals and parameters.

    The restriction is onerous. Often I want to transform the constructor
    parameters to pass to the super constructor.

    Oddly you are allowed method calls in the superconstructor, which
    could do bloody well anything.
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    Every compilable program in a sense works. The problem is with your unrealistic expections on what it will do.
    Roedy Green, Feb 10, 2010
    #1
    1. Advertising

  2. On Feb 10, 1:43 am, Roedy Green <>
    wrote:
    > On Tue, 09 Feb 2010 16:22:35 +0000, Steven Simpson <>
    > wrote, quoted or indirectly quoted someone who said :
    >
    > >Could that restriction not be loosened in a compiler-verifiable way,
    > >i.e. check that no statement prior to super() uses 'this' (explicitly or
    > >implicitly)?

    >
    > It would have to chase any  method calls recursively. They might
    > dynamically load code.  I don't think you can tell in general.


    I think you can. You don't have to chase anything: if you encounter
    this.foo() or super.foo() or bar(this), the code is illegal, else it's
    not. There's no other way for method calls to access the 'this'
    reference. Of course besides method calls you have to guard against
    other expressions involving this, like baz = this, quux == this and so
    on, but those are easy.

    > Perhaps you could allow an expression involving only literals, static
    > finals and parameters.


    That would be sensible.

    > The restriction is onerous.  Often I want to transform the constructor
    > parameters to pass to the super constructor.


    I often do, too. Also try-catch-rethrow is quite a common need.

    > Oddly you are allowed method calls in the superconstructor, which
    > could do bloody well anything.


    You're right, I never thought about that! Methods can be overridden in
    the subclass and access fields that have not yet been initialized, if
    they are called from the super constructor. One more sign that
    disallowing statements before super() does not guarantee any
    additional safety.

    Alessio
    Alessio Stalla, Feb 10, 2010
    #2
    1. Advertising

  3. Roedy Green

    Pitch Guest

    In article <b366f0eb-11ae-48bb-86b1-67eff9b81a88
    @k41g2000yqm.googlegroups.com>, says...

    > > Oddly you are allowed method calls in the superconstructor, which
    > > could do bloody well anything.

    >
    > You're right, I never thought about that! Methods can be overridden in
    > the subclass and access fields that have not yet been initialized, if
    > they are called from the super constructor. One more sign that
    > disallowing statements before super() does not guarantee any
    > additional safety.



    yep.



    --
    stirr your cofee properly
    Pitch, Feb 10, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Guest

    super.super.super how?

    Guest, Feb 19, 2005, in forum: Java
    Replies:
    24
    Views:
    10,737
    Darryl Pierce
    Feb 24, 2005
  2. Fernando Rodriguez

    Getting the super class via the super() function

    Fernando Rodriguez, Nov 21, 2003, in forum: Python
    Replies:
    2
    Views:
    708
    Bob Willan
    Nov 22, 2003
  3. Kerim Borchaev

    super. could there be a simpler super?

    Kerim Borchaev, Jan 15, 2004, in forum: Python
    Replies:
    4
    Views:
    466
    Michele Simionato
    Jan 15, 2004
  4. Lew
    Replies:
    34
    Views:
    855
    Tom Anderson
    Feb 13, 2010
  5. Mike Schilling

    Re: Statements before super()

    Mike Schilling, Feb 9, 2010, in forum: Java
    Replies:
    9
    Views:
    310
Loading...

Share This Page