-2

Why my name cannot get object name by using this?

public void setPhoneNumber(String n){
        phone_number1 = n;
    }

    public void setEmail(String n){
        email = n ;
    }

    public Person (String name, String address, String phone_number1, String email){
        this->name = name;
  getName();
        setAddress(address);
        setPhoneNumber(phone_number1);
        setEmail(email);
    }

    public Person (String name){
        getName();
    }
}
Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85
jianwei
  • 3
  • 5

2 Answers2

1

As a C++ programmer, you should already have the basic idea of object-oriented programming, and the Java syntax in most cases should be familiar to you.

But in Java the equivalent of C++ arrow operator is .

The dot operator in Java is used almost for everything, just use:

this.name = name;

In Java arrow operator is used only in lambda expression.

The use of the this reserved keyword, is the same of C++.

It can be used inside the Method or constructor of Class. this works as a reference to the current Object whose Method or constructor is being invoked. The this keyword can be used to refer to any member of the current object from within an instance Method or a constructor.

freedev
  • 25,946
  • 8
  • 108
  • 125
0

this->name = name; change to this.name = name;

Mehraj Malik
  • 14,872
  • 15
  • 58
  • 85