# Thread: How to use one class' objects as variable for other class

1. Member
Join Date
Apr 2016
Posts
27
Rep Power
0

## How to use one class' objects as variable for other class

Hi guys. I think I have a fairly easy question on objects and variables. I got three classes, one main class (class with main method) and the two other classes called Region and Train.
The second class is named is Region. Each Region-object has its own variables, and some variables I will use to make calculations with. Examples are the variables averageSpeedInRegion, averageNumberOfCustomers, etc.
In the class with my main method I want to assign trains to specific regions. Trains are assigned to specific zones, and based on their regions, they will take a specific time to go from one location to the other (based on their average speed).

How do I assign a train to a specific Region, and thus give it an average speed based on this region?

Example:
Region A1
Averagespeed = 40 mph

Train T1 rides a track in region A1. Average speed is 40 mph in this region, thus averagespeed of train T1 is 40 mph.

2. Moderator
Join Date
Apr 2009
Posts
13,541
Rep Power
27

## Re: How to use one class' objects as variable for other class

I would say the Train should know what region it is in, so should have a reference to the Region.
When a Train is added to a Region it would (in my mind) be added to a Set<Train> in the Region, and the Region would setRegion on the Train, passing itself in.
The Train would then be able to request the average speed off the Region.

This is off the top of my head, so treat with suspicion!
:)

3. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: How to use one class' objects as variable for other class

Originally Posted by casta
How do I assign a train to a specific Region, and thus give it an average speed based on this region?
You don't! You assign a region to a train (like Toll's said). Then the train simply interrogates its region object reference to
determine how it is to operate in that region.

Regards,
Jim

4. Member
Join Date
Apr 2016
Posts
27
Rep Power
0

## Re: How to use one class' objects as variable for other class

Okay. How would I do that? In my mind, I would do something like this:

Java Code:
```public class Region {

String regionName;

double speedInRegion;

static void initiateRegions(){
Region CaW = new Zone();
CaW.regionName = "Carolina West";
CaW.speedInRegion = 40.00;```
How would I refer to the correct region? What I wrote below certainly does not work
Java Code:
```		Region.initiateRegions(); //Create all regions with their corresponding speeds
Train t1 = new Train();
t1.regionActive = CaW;```

5. Senior Member
Join Date
Jan 2013
Location
Northern Virginia, United States
Posts
6,226
Rep Power
14

## Re: How to use one class' objects as variable for other class

Let me amend what I said. Let's say you have a region class. You can either add the train to a specific region and then within that region, let the train obtain an instance of the region. Or you can just add an instance or the region to the train. Either way you need to know ahead of time which train will operate in which region.

So your train class would need a method which take a region object as an argument.

Regards,
Jim

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•