Receive variables from other classes

Asked

Viewed 1,143 times

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.

  • Put the full code is easier to answer. Take the opportunity to familiarize yourself with the site on tour

  • Could you explain to me how to call the variable with public access? No problem for now. @bigown:

  • 1

    @Luisa Gustavovieira you only need to put the access modifier public when declaring the variable.

  • The IDE gets and sets for you, which Voce IDE uses?

  • I edited the post because I couldn’t comment on the well formatted @rray code

  • Eclipse @Davidschrammel

  • 1

    Welcome to the Java. =]

  • Make the eclipse generate these codes, the keyboard can do, ctrl+3 typhoon ggas mark the desired options and ok.

  • Then click with the right mouse button -> Source -> Generate Getters and Setters...

  • 1

    Dude...how have I never been told this before? Much easier. Thank you!

  • @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?

Show 7 more comments

2 answers

2

  • I get it. That’s the easiest way. It’s pq I had an idea in my head where it would be possible to: Declare the variable as you did, and instead of instantiating it the way you did, simply put the variable name 'link' in another class and it recognizes it as a publish variable with value 'abc.com.br'. But I guess I dreamt it out loud right? hahah Anyway, thank you so much!

  • 1

    Or you switch to C# and have the best of both worlds :P

  • Don’t listen to @bigown, come to php! D

1

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.