12 Replies - 402 Views - Last Post: 26 July 2011 - 01:55 PM Rate Topic: -----

#1 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 395
  • Joined: 17-February 11

Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:09 PM

I had a program with four classes: the game launcher (main), the game, the player, and a vehicle class. I had to remove the vehicle class because it wasn't working well with the player class. All of the code in the game class is in a public void method (startGame) that was called by the static game launcher. The vehicle class was instantiated in the startGame() method.

What I wanted to do was use a variable from the vehicle class (vehicle1.fuel) in a method called showInventory(). I'll try to simplify the code from the Player class:

public class Player {
     
     int money = 500;
     int resources = 100;

     public void showInventory() {
         System.out.println("Money: " + money);
         System.out.println("Resources: " + resources);
         System.out.println("Fuel: " vehicle1.fuel); // <-- How would I do this correctly?
     }
}



I have no idea what I should do. I tried instantiating the vehicle class in the Player class as well, and that made it work, but I know that would not end up working when I begin changing the value of vehicle1.fuel.

This post has been edited by AVReidy: 26 July 2011 - 01:16 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Why can't variables transfer across certain classes?

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9156
  • View blog
  • Posts: 33,973
  • Joined: 27-December 08

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:14 PM

First, your instance fields should be private and you should have getter/setter methods. This goes towards encapsulation. Then just invoke getFuel(), or getWhatever() for the appropriate method on the Vehicle object.

What specifically about what you were doing wasn't working? What problems or errors were you encountering?
Was This Post Helpful? 0
  • +
  • -

#3 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 395
  • Joined: 17-February 11

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:21 PM

The specific error was just that the compiler couldn't find the Vehicle class's fuel variable.

Would it be better like this?

public class Player {
     
     private int money;
     private int resources;

}


This post has been edited by AVReidy: 26 July 2011 - 01:29 PM

Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9156
  • View blog
  • Posts: 33,973
  • Joined: 27-December 08

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:24 PM

Obviously the Vehicle class didn't have an instance or static variable named fuel in the scope that the other classes could access. Without seeing your Vehicle class, we are the blind leading the blind.
Was This Post Helpful? 0
  • +
  • -

#5 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 395
  • Joined: 17-February 11

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:33 PM

Just say the vehicle class is this:
(I didn't get far before I realized it doesn't work)


public class Vehicle {
    
    private String vehicleName;
    int fuel;
    
}



Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9156
  • View blog
  • Posts: 33,973
  • Joined: 27-December 08

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:34 PM

Let's do this- show me your setup (code) so I can see what's going on, and post your errors as well.
Was This Post Helpful? 0
  • +
  • -

#7 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 395
  • Joined: 17-February 11

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:43 PM

I think I'll write it with getter/setter methods and try to encapsulate things like you said. It wasn't a big problem to the code because I could just put the fuel variable in the player class. Could you give me an example of how I would use a getter/setter method?

I could make a method in the Vehicle class that prints the value for fuel... but I don't know if I could apply getter/setter.
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9156
  • View blog
  • Posts: 33,973
  • Joined: 27-December 08

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:44 PM

Why not?
class Vehicle{
    private int fuel;

    public int getFuel(){ return fuel; }
    public void setFuel(int fuel){ this.fuel = fuel; }
}



For more on class design, see my tutorial Moving Away From Parallel Arrays.
Was This Post Helpful? 2
  • +
  • -

#9 jimdandy75  Icon User is offline

  • D.I.C Regular

Reputation: 37
  • View blog
  • Posts: 310
  • Joined: 30-June 08

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:44 PM

Yeah, like mac said make all your instance variables private. Including fuel! Then create a method in your Vehicle class that returns the fuel amount, like:
public int fuelAmount(){
return fuel;
}


Then in your Player class you would call the method on your vehicle1 object like this:
System.out.println("Fuel: " vehicle1.fuelAmount());



// mac beat me to the punch :-)

This post has been edited by jimdandy75: 26 July 2011 - 01:45 PM

Was This Post Helpful? 2
  • +
  • -

#10 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 395
  • Joined: 17-February 11

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:49 PM

Now how would I set a fuel amount from the game class if I were to use some up or buy more? Would I need to use a constructor or something?
Was This Post Helpful? 0
  • +
  • -

#11 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9156
  • View blog
  • Posts: 33,973
  • Joined: 27-December 08

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:50 PM

No. Just invoke the setFuel() method on the Vehicle class and pass in the new fuel amount.
myVehicle.setFuel(20);


Was This Post Helpful? 0
  • +
  • -

#12 AVReidy  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 395
  • Joined: 17-February 11

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:55 PM

I'm such a newb. Didn't even know you could do that, but it seems very natural... Thanks though, I learn something new from you guys with every post! :lol:
Was This Post Helpful? 0
  • +
  • -

#13 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 9156
  • View blog
  • Posts: 33,973
  • Joined: 27-December 08

Re: Why can't variables transfer across certain classes?

Posted 26 July 2011 - 01:55 PM

Glad we could help! :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1