0

I have two classes, the User class which has the User properties which has getters/setters and a contructor, and the UsersInLondon class, they both reside in the same package. I'm having problems with this bit of code, in the UsersInLondon class:-

if (distanceInMiles <= 50)
{
  System.out.println("People who live in London");
  User user = new User();
  user.setId(id); 

/.... etc I use IntelliJ, and it is not picking up the User properties in the User class, the property id is in red. I know how to instantiate a class but I've never encountered this before, the error message is: Error:(63, 38) java: cannot find symbol symbol: variable id location: class com.londonusers.UsersInLondon Any help, would be much appreciated. Thanks

  • Can you show where you declare "id" variable? (int id = ...) – Andrew Sulyz Jun 16 '20 at 14:53
  • Yes, this is declared in the User class, along with all the other properties of User, like this: public class User { private int id; as stated I've generated getters/setters, and there is a constructor as well. Thanks – MindGames2015 Jun 16 '20 at 14:58
  • @MindGames2015 Andrew Sulyz is not speaking about the user _field_ with the name `id`, but about the the _variable_ with the name `id` in the statement `user.setId(id)`! Where does that come from? Obviously you did not declare that. – Seelenvirtuose Jun 16 '20 at 15:10
  • Please edit the question and post a [mcve] – OldProgrammer Jun 16 '20 at 15:14
  • OK, so the variable id is not declared in UsersInLondon, I thought the getters/setters would pick up the id, from the instance user, when you write user.setId(id), and if you use the getter method the same but written like this user.getId(). Thanks :-) – MindGames2015 Jun 16 '20 at 15:16
  • 1
    Why even do that? It's redundant to set the user id ... to the same user id it already was. What's the point ... of saying `user.setId(user.getId());` ... it's redundant. And no, it doesn't go looking in User class for variable id ... that's why you have a getter method, so you can call it and get the id. But do you see the redundant, circular logic you're creating here ... set the id in User to the same thing the id already is in User? Makes no sense. – Lotsa Jun 16 '20 at 15:30
  • Yes I've seen the error of my ways, my mind is mush at the moment, so thanks for pointing out my mistake, and thanks for all your comments, much appreciated. I've sorted this out now. :-) – MindGames2015 Jun 16 '20 at 15:38

1 Answers1

-1

It seems the "id" specified in the below statement is not declared and but used.
user.setId(id);