1
A class that contains variables and their values, and needs some simple way to feed the other class with these variables.
For example, I have a class with the variable String urlPage = 'www.abc.com'
. That one urlPage
can change sometimes, but I didn’t want to change in all classes that use this value, I wanted to change only in the class where it is defined. So, in the other classes, in their proper methods, I would only inform the variable, and they would already understand that its value is 'www.abc.com'.
I made it work with get
and set
, but I wanted to know if there is a simpler way, because it would not be just a variable, it would be several, and I think it would be a job to make a get
and set
for each one.
I did the following:
public class UsedLinks {
private String homepagelink = "abc.com.br";
public void setHomepage( String homepage)
{ this.homepagelink= homepage; }
public String getHomepage()
{ return this.homepagelink; } }
Already in the other class, I just called
Usedlinks link = new Usedlinks();
link.gethomepage();
Only I found a lot of work being that I will use not only the URL as variable.
Show what you did. But at first this is right. Unless you prefer to make the variable public access. Not recommended, but can be used.
– Maniero
Put the full code is easier to answer. Take the opportunity to familiarize yourself with the site on tour
– rray
Could you explain to me how to call the variable with public access? No problem for now. @bigown:
– Luís Gustavo Vieira
@Luisa Gustavovieira you only need to put the access modifier
public
when declaring the variable.– Jéf Bueno
The IDE gets and sets for you, which Voce IDE uses?
– David Schrammel
I edited the post because I couldn’t comment on the well formatted @rray code
– Luís Gustavo Vieira
Eclipse @Davidschrammel
– Luís Gustavo Vieira
Welcome to the Java. =]
– RafaelTSCS
Make the eclipse generate these codes, the keyboard can do,
ctrl+3
typhoonggas
mark the desired options and ok.– rray
Then click with the right mouse button ->
Source
->Generate Getters and Setters...
– David Schrammel
Dude...how have I never been told this before? Much easier. Thank you!
– Luís Gustavo Vieira
@Luisa Gustavovieira Take a look at [tour]. You can accept an answer if it solved your problem. You can vote on every post on the site as well. Did any help you more? Need something to be improved?
– Maniero