Look at these two methods:
public String setNome (String newNome)
{
this.nome = newNome;
}
And:
public String Sobrenome (String newSobrenome)
{
this.sobrenome = newSobrenome;
}
Both declare return String
, but they have no kind of instruction return
. The compiler will not like this and will complain that the return
. Bluej will show you this bug he gets from the compiler.
The return of both should be void
. In addition, the method for defining seobrenome should be called setSobrenome
, and not just Sobrenome
.
Your code doesn’t follow well either conventions of the Java language (even if we consider that what actually happens is that there are some unofficial conventions competing with each other). In particular, the field AnoDeNascimento
should be called anoDeNascimento
, starting with lower case letter.
In addition, the parameter name in the method setAnoDeNascimento
could be newAnoDeNascimento
, instead of anonas
. The reason is that abbreviating words by eating letters of the name is a bad programming practice and tends to make programs very difficult to understand. In fact, to avoid mixing words in English with words in Portuguese in the same name, we could call novoAnoDeNascimento
. The other parameters of the setters would be novoNome
and novoSobrenome
. Mixing Portuguese and English by prefixing get
and set
, Hence it is better to be tolerated because many tools seek methods with prefixes get
and set
to identify the properties of your objects, and by changing these prefixes, these tools will not work with your class (which is another reason to call your method to define the last name of setSobrenome
).
Another detail is that in no widely used code formatting convention, space is used between the method name and the (
of the following parameters. The blank line between the class declaration and the {
also not something that is in accordance with any code formatting convention.
Note also that in your method getNomeCompleto()
, it is a good idea to add a space between the name and the surname.
Finally, you may have seen in several places that maintaining public attributes/fields is considered a bad programming practice, and the reason you’re using getters and setters has directly to do with it. Therefore, they must be private.
By correcting these details, your code looks like this:
public class Pessoa
{
private String nome;
private String sobrenome;
private int anoDeNascimento;
public void setNome(String novoNome)
{
this.nome = novoNome;
}
public String getNome()
{
return nome;
}
public void setSobrenome(String novoSobrenome)
{
this.sobrenome = novoSobrenome;
}
public String getSobrenome()
{
return sobrenome;
}
public int setAnoDeNascimento(int novoAnoDeNascimento)
{
this.anoDeNascimento = novoAnoDeNascimento;
}
public String getNomeCompleto()
{
return nome + " " + sobrenome;
}
}
What "is not working"?
– user28595
The bluej keeps complaining about public String of the methods setNome, and setAnoDeNascimento
– Ogarov
Complaining is very general, you need to detail this.
– Maniero
Because the signature of the two methods require return but you return nothing. Methods setters normally return nothing, are void;
– user28595
Got it, thank you very much helped me a lot
– Ogarov
I think someone already told you that you’re not supposed to use public attributes, right?
– Victor Stafusa
@I could post a reply to our friend saying this, but as you saw first, I think you’d better go through with it.
– Victor Stafusa
@Victorstafusa if you want to answer with something more elaborate, can answer, I’m terrible at elaborating theoretical or canonical answers, as this question asks.
– user28595
@Articuno Feito.
– Victor Stafusa