Results 1 to 7 of 7
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default Access object variables

    I have created an object array using polymorphism and I would like to access those variables from my GUI class.

    for instance vehicle[1].millage
    the millage int variable is public in my vehicle class and car extends vehicle.

    is this possible?

    Java Code:
    public class Main {
        public static void main(String[] args) {
            
            new GUI();
    
            Vehicle[] vehicle = new Vehicle[3];
            vehicle[0] = new Truck();
            vehicle[1] = new Car();
            vehicle[2] = new Bike();
        }
    }

  2. #2
    ClickerMonkey is offline Member
    Join Date
    Jan 2011
    Posts
    12
    Rep Power
    0

    Default

    You would need to instantiate the Vehicle array first, and pass it to the GUI class... or create a setter in the GUI class.

    Java Code:
            Vehicle[] vehicle = new Vehicle[3];
            vehicle[0] = new Truck();
            vehicle[1] = new Car();
            vehicle[2] = new Bike();
            new GUI(vehicle);
    or:
    Java Code:
            GUI gui = new GUI(vehicle);
            Vehicle[] vehicle = new Vehicle[3];
            vehicle[0] = new Truck();
            vehicle[1] = new Car();
            vehicle[2] = new Bike();
            gui.setVehicles(vehicle);

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Java Code:
            Vehicle[] vehicle = new Vehicle[3];
            vehicle[0] = new Truck();
            vehicle[1] = new Car();
            vehicle[2] = new Bike();
            new GUI(vehicle);
    Create a constructor in GUI?
    I do not understand how I would do this.

    Java Code:
            GUI gui = new GUI(vehicle);
            Vehicle[] vehicle = new Vehicle[3];
            vehicle[0] = new Truck();
            vehicle[1] = new Car();
            vehicle[2] = new Bike();
            gui.setVehicles(vehicle);

    It seems that only in the main static method I can access the variables, probably because I instantiated them there. But how to I instantiate the SAME object so it doesnt make a new one.

    I am just not sure how to make the constructor in GUI.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    You don't instantiate the same object. You instantiate an object and then pass its reference around.

    So the call to setVehicles passes the reference to your array to the GUI object.
    Presumably the GUI object would then set an attribute equal to that reference:
    Java Code:
    public void setVehicles(Vehcile[] vehicles) {
         this.vehicles = vehicles;
    }
    This presumes that GUI has an attrbiute vechiles of type Vehicle[].

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Quote Originally Posted by Tolls View Post
    You don't instantiate the same object. You instantiate an object and then pass its reference around.

    So the call to setVehicles passes the reference to your array to the GUI object.
    Presumably the GUI object would then set an attribute equal to that reference:
    Java Code:
    public void setVehicles(Vehcile[] vehicles) {
         this.vehicles = vehicles;
    }
    This presumes that GUI has an attribute vehicles of type Vehicle[].

    I have code in the main method (the static method) that manipulates the reference specifically the vehicles[]. The reference Object is made in a static method (main). When I try to reach it from a non static method / class I get an error.

    I guess I am just not understanding. Can you access the variables made in a static method main like a public variable or objects?

  6. #6
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    4

    Default

    Bump....

    So can you call variables or references created in a static method, in my case the main method, is it possible. I do not understand the last answers.

    thank you all for your help.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,173
    Rep Power
    20

    Default

    No you can't, because they are only within the scope of that method.

    However, you can pass the reference around (as I showed above).
    So pass the Vechile[] reference to GUI, either through the constructor or through a setVehicle method.

Similar Threads

  1. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 5
    Last Post: 07-08-2010, 11:50 AM
  2. Access Public Global Variables in class
    By spatel14 in forum New To Java
    Replies: 1
    Last Post: 07-07-2010, 08:41 PM
  3. How to manage/access variables between classes
    By dan0 in forum New To Java
    Replies: 2
    Last Post: 04-03-2009, 01:53 AM
  4. Replies: 3
    Last Post: 03-14-2009, 08:22 AM
  5. Object access between classes
    By qazplm123890 in forum AWT / Swing
    Replies: 1
    Last Post: 03-05-2009, 06:00 AM

Posting Permissions

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