this
β this
this
refers to the field of the current classthis
is to distinguish which isfield
and which isparamter
when the variable names are the samethis.name
: thename
field of this classname
: thename
parameter for methodinitMember
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 ofmember1
1
2
MemberInit member1 = new MemberInit2();
member1.initMember("member1", 15, 90);
- βοΈ so if
field name
andparameter
is different, no need forthis
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 isfield
and which isparamter
- βοΈ 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.