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?

  • 2
    Then you have to write like that. 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
  • I understand that after this entry { this.name = name. } { this.name = name. } link name will refer to the field of the String name instance. But why is this needed if public Employee(String name) already indicates the type of this link? - Kojer Defor
  • @ user31238 The compiler is able to distinguish the name = name construction correctly, this specifies more for the person, so that he would understand. - pavlofff
  • @pavlofff readability - our everything - user31238
  • The @KojerDefor argument is assigned to the class field so that it is available everywhere in the class, and not only in the constructor to which it is passed (the variable passed through the constructor will be available only inside the constructor. Read more about the scope of variables in Java). Accordingly, if you do not need this value outside of the constructor, then there is no need to assign it to the class field. - pavlofff

2 answers 2

The exact name of the constructor parameter (as opposed to the type) does not matter. In the second listing, the value passed in the name parameter will not be written to the Employee object field. Add a call to display the value of the name field after creating instances of the class in each case, for example, for the second case:

 public class TestEmployee { public static void main(String[] args) { String name = "larry"; Employee harry = new Employee(name); System.out.println(harry.name); } } class Employee { String name; public Employee(String name) { System.out.println("constructor"); } } 

In order to visually distinguish the name of the parameter and the name of the class field, the parameter was called n , it could also be called name , but then it would be more difficult to visually distinguish where what. In any case, the compiler would successfully parse such a construct and compile the class.

The main thing that you should understand from this example is that simply by naming the constructor parameter as well as the class field, you will not achieve automatic assignment of values!

  • one
    Thanks for the good answer. - Kojer Defor
  • In fact, this is all a matter of the scope of variables in Java, which extends only to curly brackets, where this variable is defined - this is all explained - pavlofff

In the declaration of the constructor of this class

 class Employee { String name; public Employee(String name) { System.out.println("constructor"); } } 

The constructor name parameter is a local constructor variable that hides the class field of the same name. This field is not initialized by the constructor, therefore the field of the class name will be null . In this case, it is pointless to specify the parameter at the constructor. It would be correct to write

  public Employee(String name) { this.name = name; System.out.println("constructor"); } 

If you still need to define a default constructor, that is, a constructor that is called without arguments, for example, you can write

 class Employee { String name; public Employee(String name) { this.name = name; System.out.println("constructor with parameter"); } public Employee() { this.name = ""; System.out.println("default constructor"); } }