Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing Variables from 1 class to another.

 
Rik Ny
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rik Ny
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So there's a way. But the question was how??
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Marilyn de Queiroz
Sheriff
Posts: 9067
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rik Ny
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey christopher, if you wouldn't mind could I have some more details on how to implement your theory. Cheers.
 
William Barnes
Ranch Hand
Posts: 986
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic