Web Hosting, Design, & Coding forum

General discussion

Java Question - Variables

by StargateFan / January 23, 2008 4:52 AM 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
You are posting a reply to: Java Question - Variables
The posting of advertisements, profanity, or personal attacks is prohibited. Please refer to our CNET Forums policies for details. All submitted content is subject to our Terms of Use.
Track this discussion and email me when there are updates

If you're asking for technical help, please be sure to include all your system info, including operating system, model number, and any other specifics related to the problem. Also please exercise your best judgment when posting in the forums--revealing personal information such as your e-mail address, telephone number, and address is not recommended.

You are reporting the following post: Java Question - Variables
This post has been flagged and will be reviewed by our staff. Thank you for helping us maintain CNET's great community.
Sorry, there was a problem flagging this post. Please try again now or at a later time.
If you believe this post is offensive or violates the CNET Forums' Usage policies, you can report it below (this will not automatically remove the post). Once reported, our moderators will be notified and the post will be reviewed.
Collapse -
Re: Java variables
by Kees Bakker / January 23, 2008 5:26 AM 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?
by StargateFan / January 23, 2008 5:45 AM PST
In reply to: Re: Java variables
Collapse -
And what would be a better option then using global ....
by StargateFan / January 23, 2008 5:49 AM PST

variables?

Collapse -
That's a Software Engineering principle ...
by Kees Bakker / January 23, 2008 4:39 PM 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
by DocGSXR / February 17, 2008 12:31 AM 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.

Popular Forums
icon
Computer Newbies 10,686 discussions
icon
Computer Help 54,365 discussions
icon
Laptops 21,181 discussions
icon
Networking & Wireless 16,313 discussions
icon
Phones 17,137 discussions
icon
Security 31,287 discussions
icon
TVs & Home Theaters 22,101 discussions
icon
Windows 7 8,164 discussions
icon
Windows 10 2,657 discussions

Does BMW or Volvo do it best?

Pint-size luxury and funky style

Shopping for a new car this weekend? See how the BMW X2 stacks up against the Volvo XC40 in our side-by-side comparison.