wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Passing Variables from 1 class to another. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Passing Variables from 1 class to another." Watch "Passing Variables from 1 class to another." New topic
Author

Passing Variables from 1 class to another.

Rik Ny
Greenhorn

Joined: Oct 13, 2001
Posts: 13
How do i pass the variables "mname","mtype", "mID" ect out of this class and into another class
where it is required. I require them in an abstract class where they will be printed to a file.
I need a solution ASAP. Cheers, here is my problem.....
class MJInput
{
public void InputHere()
{
BufferedReader userinput = new BufferedReader(new InputStreamReader(System.in));
try
{
System.out.println("------------Military Jet Information------------\n");
System.out.println("Welcome, please answer the following questions:-\n");
System.out.print("Aircraft Name:- ");
String mname = userinput.readLine();
System.out.print("Aircraft Type:- ");
String mtype = userinput.readLine();
System.out.print("Aircraft ID Number:- ");
int mID = Integer.parseInt(userinput.readLine());
System.out.print("Aircraft Capacity:- ");
int mcapacity = Integer.parseInt(userinput.readLine());
System.out.println();
userinput.close();
}
catch(IOException ioe)
{
System.out.println("Error:- " + ioe);
}
}
}
Michael Bruesch
Ranch Hand

Joined: Sep 23, 2001
Posts: 158
There are numerous ways to pass these variables to a different class, some may be more appropriate than others. I come across this problem too sometimes, and I "cheat", I'll create a method who's sole purpose is to take parameters and assign them to instance variables in the other class so that my work can continue, but they're not constructors. I think that's "cheating" at least, it feels like it.
------------------
Michael J Bruesch
Codito, ergo sum...
I code, therefore I am.
My Java Games, I'm quite proud


Michael J Bruesch<br /><i>I code, therefore I am.</i>
Rik Ny
Greenhorn

Joined: Oct 13, 2001
Posts: 13
So there's a way. But the question was how??
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

I would be included to make a class who's only job is to hold the data you want to pass around. So you have a class called "AircraftData", pass that class into MJInput, fill in the values and return it. Than you can pass this class onto any other class which needs to make use of the information. This solution is a little more OO, I think.
Do you need more implimentation details, or does that work for you?


Please ignore post, I have no idea what I am talking about.
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9044
    
  10
Store the values in private variables. Use getters and setters. Have the other class use the getters and setters to "get" the values from the first class.

JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Rik Ny
Greenhorn

Joined: Oct 13, 2001
Posts: 13
Hey christopher, if you wouldn't mind could I have some more details on how to implement your theory. Cheers.
William Barnes
Ranch Hand

Joined: Mar 16, 2001
Posts: 984

Seeing Marilyn's post I like that one better.
But here is more detail on mine as you asked.
First make class which only holds the data you care about (this is sudo-code:

So that is the class to hold the data. Pass that class into the class that collects the data, fill it up with data using the "set" methods. Than pass the class into the class that prints the data, using the "get" methods to extract the data.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Passing Variables from 1 class to another.
 
Similar Threads
Runtime.getRuntime() help plzzzzzzzz
Passing Variables from 1 class to another.
HELP
HelP!
How close am I in getting this program right?