one.
public class TestEmployee { public static void main(String[] args) { String name = "larry"; Employee harry = new Employee(name); } } class Employee { String name; public Employee(String n) { name = n; System.out.println("constructor"); } } 2
public class TestEmployee { public static void main(String[] args) { String name = "larry"; Employee harry = new Employee(name); } } class Employee { String name; public Employee(String name) { System.out.println("constructor"); } } I can not understand a simple thing. When creating objects in parentheses, the arguments are specified. Depending on these arguments, the desired constructor is selected. The tutorial gives an example of the first listing. Why specify a certain reference n , when you can just write the name and the same constructor is called?
public Employee (String name) { this.name = name; }public Employee (String name) { this.name = name; }It will be just a match of the names and the argument will "close" the Name of the implicit argument. It is necessary to specify this. - user31238{ this.name = name. }{ this.name = name. }linknamewill refer to the field of theString nameinstance. But why is this needed ifpublic Employee(String name)already indicates the type of this link? - Kojer Deforname = nameconstruction correctly,thisspecifies more for the person, so that he would understand. - pavlofff