简体   繁体   中英

what is this() ? can it have multiple parameters?

I encountered a code where this() method in java takes three parameters two being integers and the third one is boolean value. what exactly does that mean ? Are there any other variants of this() method ? Hera is the actual code.

 public SegmentConstructor(int seqNum_, int length_) {
        this(seqNum_, length_, false);
    }

Thank You..

It means that there is another constructor in the current class that has that signature.

public SegmentConstructor(int seqNum_, int length_) {
    this(seqNum_, length_, false); // calls the constructor below.
}

public SegmentConstructor(int seqNum_, int length_, boolean required_) {
    seqNum = seqNum_;
    length = length_;
    required = required_;
}

The this method is just a way to call one of your class's constructors from within another constructor, to help avoid code duplication. It can only be called on the first line of a constructor--never from within any other method.

this simply invokes another constructor to run. So, look for other constructors with that signature.

As said before this invokes another constructor, mostly as a convenience method.

Trivial example:

class A {
 private int value;

 public A(int val) {
  value = val;
 }

 public A() {
  this(0); //0 as default
 }
}

Normally you do use calls to this() when the most specific constructor (that one with the most parameters) is not just assignment but contains more logic that you don't want to repeat/copy etc.

Just because it fits in here: super() can have parameters, too, ie this calls a super class' constructor with parameters from the sub class' constructor.

It is a constructor call. If your class implements different constructors with a differing number of arguments, you can chain your constructors like this:

class A {
    public A(boolean arg) {
        ...
    }

    public A() {
        this(false); // invokes the constructor with the boolean argument
    }
}

Sometimes it makes sense to create a private constructor taking different arguments and provide public and/or protected constructors with other/fewer arguments and delegate object construction to that private constructor.

It is important to know that no other code may be placed before the call to this(...). However, after calling this(...), you can do everything you could in any other constructor.

Edit: Since this(...) calls a constructor, it can only be called from within other constructors (belonging to the same class).

class MyClass { private int var1; private int var2; private boolean flag;

public MyClass(int var1_,int var2_) { this(var1_,var2_,false); }

public MyClass(int var1_,int var2_,boolean flag_) { var1 = var1_; var2 = var2_; flag = flag_; }

public String toString() { return (new Boolean(flag).toString()); }

public static void main(String[] args) { MyClass my = new MyClass(5,6); System.out.println(my); }

}

So it works.

this() is not a method but is a reserved keyword pointing to a overloaded constructor of the same class. The number of parameters you pass should point to an existing corresponding constructor defined in the class.

The super() also has the semantics however the constructor is defined in one of its parent hierarchy.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM