Search code examples
javaintellij-ideagetter-setter

How to Correctly use Getter on this Situation


i've been looking the entire day for a solution to this, but first of all, i want to say im completely new to java and object orientation, so please go easy on me

Then, here is some code i've made so far

Main Class

package br.edu.fatecmm.study;

public class Main {

    public static void main(String[] args) {

        Pessoa ppl = new Pessoa();
        Conta gamer = new Conta();

        gamer.setLogin("kouhei");
        gamer.setPassword("mypass");

        ppl.setNome("Qualquer");
        ppl.setSobrenome("Nome");
        ppl.setJogador(gamer);

        ppl.imprimePessoa();
        gamer.imprimeConta();

    }
}

Pessoa Class

package br.edu.fatecmm.study;

public class Pessoa {
    private String nome, sobrenome;
    private Conta jogador;

    public void imprimePessoa(){
        System.out.println("nome = " + nome);
        System.out.println("sobrenome = " + sobrenome);
        System.out.println("jogador = " + jogador.toString());
        System.out.println(" ");
    }

    public String getNome() {
        return nome;
    }

    public void setNome(String nome) {
        this.nome = nome;
    }

    public String getSobrenome() {
        return sobrenome;
    }

    public void setSobrenome(String sobrenome) {
        this.sobrenome = sobrenome;
    }

    public Conta getJogador() {
        return jogador;
    }

    public void setJogador(Conta jogador) {
        this.jogador = jogador;
    }
}

Conta Class

package br.edu.fatecmm.study;

public class Conta {
    private String login, password;

    public void imprimeConta(){
        System.out.println("login = " + login);
        System.out.println("password = " + password);
        System.out.println(" ");
    }

    public String getLogin() {
        return login;
    }

    public void setLogin(String login) {
        this.login = login;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

So, what happens when i run the code described above is this:

"C:\Program Files\Java\jdk-11.0.2\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.4\lib\idea_rt.jar=49901:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2018.3.4\bin" -Dfile.encoding=UTF-8 -classpath C:\Users\Kouhei\IdeaProjects\Study\out\production\Study br.edu.fatecmm.study.Main
nome = Qualquer
sobrenome = Nome
jogador = br.edu.fatecmm.study.Conta@3498ed
 
login = kouhei
password = mypass
 

Process finished with exit code 0

Basicaly what i want to do is, when i call ppl.imprimePessoa();

It should return nome + sobrenome, which is respectively "Qualquer" and "Nome", and then return jogador, which SHOULD be "kouhei" and "mypass", however, instead of kouhei and mypass return, im getting "br.edu.fatecmm.study.Conta@3498ed"

I believe 3498ed is poiting to a memory position, but im not sure, maybe im saying something completely stupid, but i dont even know what to look for to study about this

I tried reading about Getter and Setters, but nothing helps

I also want to be able to retrieve not only "kouhei/mypass" when calling ppl.imprimePessoa();

But i would also like to be able to retrieve only "kouhei" for example

I tried this

ppl.setJogador(gamer.getLogin());

but it doesnt work

I've been trying lots of different methods for hours, and also saw something about instances, however couldnt make it work

Thanks in advance


Solution

  • When you call the default toString() what is returned is a string representation of the object.

    As explained in java docs: https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html

    If you want to use toString, you should override it in class Conta, like this:

    @Override
    public String toString(){
        return "login = " + login + "password = " + password;
    }
    

    Other possibility if you don't feel comfortable overriding a default method (no problem doing that though, really), is to create a new method that returns a String in class Conta. Like:

    public String jogadorString(){
        return "login = " + login + "password = " + password;
    }
    

    and use it in class Pessoa as jogador.jogadorString() instead of jogador.toString().