Jump to content

I am learning java and I was wondering if there is a way to have a variable that can be called upon in two separate classes. In most cases you can call upon the variable that you declared in a specific class and you can use it throughout the class. But is there a way to use that same variable though more than one class?  

Link to comment
https://linustechtips.com/topic/74908-java-question/
Share on other sites

Link to post
Share on other sites

I'm not sure if I understand the question correctly, but I can think of two ways to do this.

 

1. Create a class to hold that variable and create an instance of that class and manipulate it with other classes.

 

2. Make the variable static in a class and call upon the class to access the variable.

 

There are pros and cons to both and there are probably other ways to do this also.

Link to comment
https://linustechtips.com/topic/74908-java-question/#findComment-1051741
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×