0

I've been toying for hours, changing static, private, public etcetera :) But it still won't work. If I change static at one place, I get an error at another place etc.

I have a class called person. I've used NON-static Setters because the Person() constructor is also non-static.

public class Person {
    private String name;
    private String lastname;
    private String nickname; 

    Person() {
        this.name = ""; 
        this.lastname = ""; 
        this.nickname = "";  
    }

    public void setName(String name) {
        this.name = name; 
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;  
    }

    public void setNickname(String nickname) {
        this.nickname = nickname; 
    }
}

Then I have a file with my main method, and different methods for interacting with the user.This method is also static because it calls that methods that take the userInput which is using the Scanner class.

public class Interaction {
    public static void takeName() {
        String name; 
        String lastname;
        String nickname;  

        System.out.println("What is your firstname:");
        name = userInput(); // calls method with Scanner class
        System.out.println("What is your lastname:");
        lastname = userInput(); // calls method with Scanner class
        System.out.println("What is your nickname:");
        nickname = userInput(); 

        person.setName(name);
        person.setLastname(lastname); 
        person.setNickname(nickname); 
    }
//editor: missing closing bracket

What I've tried:

  • I've tried to dat Person.person.setname(name);
  • Declare the String in the public class Interaction, and then pass the String using this.name and call the method from the public class Interaction
  • tried to change static, private etc. etc.
  • Delete the constructor class Person() in Person class.

What am I missing here?

EDIT: I'VE ADDED SOME MORE INFO as you requested :)

My new Person object will be declared if it passes an if statement.

IF there is a place available then a new person will be created and added to this place.

public class Theater {
    void reservationSystem () {
        if (availability > 0) {
            for (int i = 0; i < freespaces.length; i++) {
                if (freespaces[i].person == null) {
                    freespaces[i].person = new Person(); 
                    break; 
                }
            }
        } else {
            System.out.println("No tickets for you today :) ");
        }
    }
//editor: missing closing bracket

So my way of thinking is:

  • I fill a constructor with the data from the Userinput() using the Scanner class;
  • and THEN I create the new Person object so it has that data!
  • When I create a new Person in the reservation system, then the data in the constructor will be filled with data AGAIN but now with new data :)

If you need any more information please let me know :)

stealthjong
  • 10,858
  • 13
  • 45
  • 84
swennemen
  • 945
  • 1
  • 14
  • 24

3 Answers3

2

The first thing to note is that your Person constructor is a little useless, you can rewrite Person as such:

public class Person {

    private String name = "";
    private String lastname = "";
    private String nickname = "";

    public void setName(String name) {
        this.name = name;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
}

Now onto your Interaction class. This needs to by public rather than Public but I assume that's a typo.

You need to have an instance of Person to call your setters on as they are instance methods. You need to somewhere call new Person().

The easiest way of writing your takeName() method is by creating a Person in the method and returning the instance:

public class Interaction {

    public static Person takeName() {
        final Person person = new Person();
        System.out.println("What is your firstname:");
        person.setName(userInput());
        System.out.println("What is your lastname:");
        person.setLastname(userInput());
        System.out.println("What is your nickname:");
        person.setNickname(userInput());
        return person;
    }
}
Boris the Spider
  • 59,842
  • 6
  • 106
  • 166
1

Your 'takeName()` function should update a single instance of the Person class.

One approach is to create the Person externally and pass it to the function:

Public class Interaction {
    public static void takeName(Person person) {
      String name; 
      String lastname;
      String nickname;  

      System.out.println("What is your firstname:");
      name = userInput(); // calls method with Scanner class
      System.out.println("What is your lastname:");
      lastname = userInput(); // calls method with Scanner class
      System.out.println("What is your nickname:");
      nickname = userInput(); 

      person.setName(name);
      person.setLastname(lastname); 
      person.setNickname(nickname); 
  }
}

But I think it would be more intuitive to create the person instance inside the function and return it:

Public class Interaction {
    public static Person takeName() {
      String name; 
      String lastname;
      String nickname;  

      Person person = new Person();

      System.out.println("What is your firstname:");
      name = userInput(); // calls method with Scanner class
      System.out.println("What is your lastname:");
      lastname = userInput(); // calls method with Scanner class
      System.out.println("What is your nickname:");
      nickname = userInput(); 


      person.setName(name);
      person.setLastname(lastname); 
      person.setNickname(nickname); 

      return person;
  }
}
Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • I disagree: the first implementation allows for using **descendant classes** of Person too - while the second one is tied to that implementation... – ppeterka Oct 04 '13 at 08:04
  • both have pros and conns, depending on what the original asker is trying to achieve. – Mureinik Oct 04 '13 at 08:06
1

Because this is not valid syntax: Person.person.setname(name);

What this would mean in this context is:

  1. get class named Person
  2. get static field of Person class named person
  3. find and invoke instance methid setname with argument name

But your Person class - appropriately - does not have a static field named person...

The root cause of your issues is most likely not being entirely familiar with the concept of classes, instances, and in connection, the meaning of static and instance members and methods...

  • Static always means the referenced part is connected with the class.
  • Whereas an instance variable or method (e.g. everything non-static) is connected to the instances of said classes.
ppeterka
  • 20,583
  • 6
  • 63
  • 78