Java constructor and variable field

Why don’t we use the constructor parameters name same as the variable field names?? I think it would be a lot more readable and understandable rather than using different names for the same thing.

You are exactly correct! That is what programmers do in Java as well as other programming languages. In java, when you are assigning the the arguments for you parameters to a variable in the constructor you can do this:

private String name = “”;
public Classname(String = name)

{
this.name = name
}

{this.name} refers to the Classname object called name and the {name} is in reference to the argument passed into the constructor.

that prefix {this.} before the variable name will always refer to variable belonging to the class object that is calling a particular function or being instantiated.

public class Dog {
  String breed;
  boolean hasOwner;
  int age;
  String color;
  
  public Dog(String breed, boolean hasOwner, int age, String color) {

    breed = breed;
    hasOwner = hasOwner;
    age = age;
    color = color;
  }
  
  public static void main(String[] args) {


    Dog fido = new Dog("poodle", false, 4, "black");
    Dog nunzio = new Dog("shiba inu", true, 12, "white");

    boolean isFidoOlder = fido.age > nunzio.age;
    int totalDogYears = nunzio.age + fido.age;
    System.out.println("Two dogs created: a " + fido.breed + " and a " + nunzio.breed + " " + fido.color);
    System.out.println("The statement that fido is an older dog is: " + isFidoOlder);
    System.out.println("The total age of the dogs is: " + totalDogYears);
    System.out.println(fido.breed);
  }
}

in the terminal window it is showing null in place of breed instead of its name. And when I make the variable field name and parameter name different then it is working absolutely fine.
please help.

Yes, you are correct. It should be fine when that happens. I am saying that in the constructor put:

this.breed = breed

That will work as well.

no I am asking why is it showing null in place of fido.breed and nunzio.breed and fido.color