calling constructors within constsructors

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Murat Tasan

    calling constructors within constsructors

    i have a class and a general constructor, then i have some other
    constructors, each of which i'd like use the general constructor.

    so in the other ones, i tried something like:

    this = MyObject();

    but that cannot work, as the variable "this" is final, so it cannot be
    assigned anything.

    can this be done (similarly to the super() call?).

    and yes, i do know that i can wrap the general constructor up into a
    private method and have each one of the constructors call that one... in
    fact this is what i've already done... but i'm just curious if what i
    wanted can be done more elegantly.

    thanks,

    murat

    --
    Murat Tasan
    mxt6@po.cwru.ed u
    tasan@eecs.cwru .edu
    murat.tasan@cwr u.edu
    Learn how the Genomics Core at Case Western Reserve University can provide you with a wide range of genomics and DNA/RNA quality control services.


  • Patrick Carl

    #2
    Re: calling constructors within constsructors

    Try the following:

    public class MyClass{
    public MyClass(){
    // simple constructor
    }

    public MyClass(String str){
    this();
    // do sth else

    }
    }

    It is important that "this()" is the first statement in your complex
    constructors.

    Patrick

    Comment

    • Murat Tasan

      #3
      Re: calling constructors within constsructors

      thanks for the suggestion, i totally forgot about that...

      but then i found out it doesn't work in my situation anyhow... to use the
      this(...) line in another constructor, it must be the first executed line
      of that method... which doesn't help me.

      so i think my alternative is the only way:

      wrap the generic constructor into a private initialization method, make
      the generic constructor only call this method, and then call this method
      from the other constructors when needed.

      thanks for the help,

      murat

      On Wed, 22 Oct 2003, Patrick Carl wrote:
      [color=blue]
      > Try the following:
      >
      > public class MyClass{
      > public MyClass(){
      > // simple constructor
      > }
      >
      > public MyClass(String str){
      > this();
      > // do sth else
      >
      > }
      > }
      >
      > It is important that "this()" is the first statement in your complex
      > constructors.
      >
      > Patrick
      >
      >[/color]

      --
      Murat Tasan
      mxt6@po.cwru.ed u
      tasan@eecs.cwru .edu
      murat.tasan@cwr u.edu
      Learn how the Genomics Core at Case Western Reserve University can provide you with a wide range of genomics and DNA/RNA quality control services.


      Comment

      • Joe

        #4
        Re: calling constructors within constsructors

        In article <Pine.SOL.4.53. 0310221659350.1 2383@stewie>,
        tasan@eecs.cwru .edu says...
        [color=blue]
        > so i think my alternative is the only way:[/color]



        More often, it works the other way around:

        public MyClass(String str){
        // set instance variable to str
        // do sth else

        }


        public class MyClass() {
        this(String defaultStringVa lue);

        }
        }



        --
        "Everybody plays the fool. Sometimes."
        -- American Folk Saying

        Comment

        Working...