Post

this

βœ… this

  • this refers to the field of the current class
  • this is to distinguish which is field and which is paramter when the variable names are the same

  • this.name: the name field of this class
  • name: the nameparameter for method initMember
1
2
3
4
5
6
7
8
9
10
11
12
public class MemberInit {
    String name; //πŸ”΄
    int age;
    int grade;

    void initMember(String name //πŸ”΅, int age, int grade){
        this.name//πŸ”΄ = name; //πŸ”΅
        this.age = age;
        this.grade = grade;
    }

}
  • in Main class to create a new member
  • this code will create a new member, with name of member1
1
2
MemberInit member1 = new MemberInit2();
member1.initMember("member1", 15, 90);
  • ⭐️ so if field name and parameter is different, no need for this
1
2
3
4
5
6
7
8
public class MemberThis {
    String nameField;

    void initMember(String nameParameter){
        nameField = nameParameter;
        //this.nameField = nameParameter; // this is also possible
    }
}
  • ⭐️ this is to distinguish which is field and which is paramter
  • ⭐️ if variable names were same in MemberThis the same code would look like this
1
2
3
4
5
6
7
public class MemberThis {
    String name;

    void initMember(String name){
        this.name = name;
    }
}

βœ… Constructor

  • πŸ‘πŸ» allows to create instance and set the fields at the same time
  • πŸ‘πŸ» if instance is created and fields are not set, compile error
  • (prevent ghost member with no fields being created)

  • constuctor name == class name
  • no return value
1
2
3
4
5
6
7
8
9
10
11
public class MemberConstruct {
    String name;
    int age;
    int grade;

    MemberConstruct(String name, int age, int grade){ //constuctor name == class name
        this.name = name;
        this.age = age;
        this.grade = grade;
    }
}
  • πŸ‘ŽπŸ» before
1
2
    MemberConstruct member1 = new MemberConstruct();
    member2.name = "member1";
  • πŸ‘πŸ» after
1
2
3
    MemberConstruct member1 = new MemberConstruct("member1", 15, 90);
    //use constructor, 그러면
    //ν•œλ²ˆμ— create class and field κ°€λŠ₯

βœ… Default contructor

  • we need a constructor to create an instance
1
MemberDefault member = new MemberDefault();
  • basic default constructor is automatically created by Java
  • has no field
1
2
3
4
5
public class MemberDefault {
    String name;

    MemberDefault(){} //basic default constructor
}
  • if there is any other constructor, basic constuctor will not be made
1
2
3
4
5
6
7
public class MemberDefault {
    String name;

    MemberDefault(String name){ //other constructor
        this.name = name;
    }
}
1
2
3
4
5
//now this is impossible
MemberDefault member = new MemberDefault(); //πŸ”΄compile error

//only this is possible
MemberDefault member = new MemberDefault("memberName"); //use other constructor

βœ… constructor overloading

  • not all fields in class has to be in constuctor
1
2
3
4
5
6
7
8
9
10
    MemberConstruct(String name, int age, int grade){
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    MemberConstruct(String name, int age){ //no grade, grade will be 0
        this.name = name;
        this.age = age;
    }
1
2
MemberConstruct member1 = new MemberConstruct("member1", 15, 90); //member1 15 90
MemberConstruct member2 = new MemberConstruct("member2", 16); //member2 16 0
  • also can call constuctor in constuctor
  • ⭐️ this() always has to come first line
1
2
3
4
5
6
7
8
9
    MemberConstruct(String name, int age, int grade){
        this.name = name;
        this.age = age;
        this.grade = grade;
    }

    MemberConstruct(String name, int age){ //call constructor in constructor
        this(name, age, 0); //⭐️first line
    }
This post is licensed under CC BY 4.0 by the author.