Receive value of variables from other classes

1

A class that contains the variables and their values, and I need some simple way to feed the other class with those variables.

For example, I have a class with the String urlPage = 'www.abc.com' variable. This urlPage can change the times, but I did not 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 due methods, I would only inform the variable, and they would already understand that its value is 'www.abc.com'.

I made this work with get and set , but I wanted to know if there is a simpler way, since it would not be just one variable, it would be several, and I think it would work to make a get e set for each one.

I've done 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(); 

But I found a lot of work being that I will not only use the URL as a variable.

    
asked by anonymous 15.10.2015 / 19:20

2 answers

2

For simplicity you can make the attribute public, so you do not need to create the pair of access methods:

public class UsedLinks {
    public String homepagelink = "abc.com.br";
}

Usage:

Usedlinks link = new Usedlinks(); 
link.homepagelink; 

There are disadvantages in doing this and should be avoided , but you're not wrong if you know what you're doing. It's not difficult to do it the right way and some IDEs even automate the process.

See more .

    
15.10.2015 / 19:35
1

I would create the object of the class in each corner that you need to change, and create a method to change the values of those that you think need to change.

    
15.10.2015 / 21:44