Thank you for being a valued part of the CNET community. As of December 1, 2020, the forums are in read-only format. In early 2021, CNET Forums will no longer be available. We are grateful for the participation and advice you have provided to one another over the years.

Thanks,

CNET Support

General discussion

Java Question - Variables

Jan 23, 2008 4:52AM PST

Here's my question, how do I call up variables that have been declared and initialized in one class in another operate class. Or more simply how do I create public variables. I am going to provide a fake yet useful program to illustrate what I want, and what I have.

public class classOne
{
public static void main(String[] args)
{
System.out.println(variableOne);
}
}
class classTwo
{
public classTwo()
{
String variableOne = "hello world";
}
}
What I want is the public class to be able to display the contents of the variable in classTwo. How do I make variableOne public? Thanks in advance

Discussion is locked

- Collapse -
Re: Java variables
Jan 23, 2008 5:26AM PST

Let me start by saying that working with public variables is considered bad programming, especially if they aren't made static so they can be changed from outside the class they are defined in.

The 'correct' way of doing this, of course, is defining a method in class 2 to tell you the value of the variable and - if necessary - change it also.

This being told, simply add the modifier public to the variable declaration. An example in http://www.glenmccl.com/tip_002.htm. That's a rather acceptable way of organising this: put all 'globals' in one module to define them.

Kees

- Collapse -
(NT) Why Exactly is it bad programming?
Jan 23, 2008 5:45AM PST
- Collapse -
And what would be a better option then using global ....
Jan 23, 2008 5:49AM PST

variables?

- Collapse -
That's a Software Engineering principle ...
Jan 23, 2008 4:39PM PST

called 'information hiding'. See, for example, http://en.wikipedia.org/wiki/Information_hiding

And another one from the good Structured Design (by Myers and Constantine): a module shouldn't depend on internals from another module.

But, of course, it's allowed and it works. Just use it if you want.


Kees

- Collapse -
Here is another option
Feb 17, 2008 12:31AM PST

public class classOne
{
public static void main(String[] args)
{
classTwo ct = classTwo.getInstance();
System.out.println(ct.getVariableOne());
}
}


public class classTwo
{
private static classTwo instance = null;
String _variableOne = "hello world";

public classTwo(){
}

public static classTwo getInstance(){
//call this instead of constructor
if(instance == null) instance = new classTwo();

return instance;
}

public void setVariableOne(String variableOne){
_variableOne = variableOne;
}

public String getVariableOne(){
return _variableOne;
}
}


Doing this will ensure that there is only one instance of your classTwo class and you can set and get your variable one. You can even create a method inside of your classTwo that does something to your variableOne and stores it into the class variable _variableOne. Then when you call the getVariableOne() method it will return the class variable _variableOne. I hope this makes sense. If you need more help let me know.