Results 1 to 7 of 7
Thread: Access object variables
- 01-07-2011, 05:54 AM #1
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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(); } }
- 01-07-2011, 05:56 AM #2
Member
- Join Date
- Jan 2011
- Posts
- 12
- Rep Power
- 0
You would need to instantiate the Vehicle array first, and pass it to the GUI class... or create a setter in the GUI class.
or:Java Code:Vehicle[] vehicle = new Vehicle[3]; vehicle[0] = new Truck(); vehicle[1] = new Car(); vehicle[2] = new Bike(); new GUI(vehicle);
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);
- 01-07-2011, 06:35 AM #3
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
Create a constructor in GUI?Java Code:Vehicle[] vehicle = new Vehicle[3]; vehicle[0] = new Truck(); vehicle[1] = new Car(); vehicle[2] = new Bike(); new GUI(vehicle);
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.
- 01-07-2011, 09:40 AM #4
Moderator
- Join Date
- Apr 2009
- Posts
- 10,452
- Rep Power
- 16
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:
This presumes that GUI has an attrbiute vechiles of type Vehicle[].Java Code:public void setVehicles(Vehcile[] vehicles) { this.vehicles = vehicles; }
- 01-07-2011, 03:37 PM #5
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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?
- 01-08-2011, 04:34 AM #6
Senior Member
- Join Date
- Dec 2010
- Location
- Indiana
- Posts
- 202
- Rep Power
- 3
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.
- 01-10-2011, 09:41 AM #7
Moderator
- Join Date
- Apr 2009
- Posts
- 10,452
- Rep Power
- 16
Similar Threads
-
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 5Last Post: 07-08-2010, 10:50 AM -
Access Public Global Variables in class
By spatel14 in forum New To JavaReplies: 1Last Post: 07-07-2010, 07:41 PM -
How to manage/access variables between classes
By dan0 in forum New To JavaReplies: 2Last Post: 04-03-2009, 12:53 AM -
Private or Protected access for super class variables
By Madushan in forum New To JavaReplies: 3Last Post: 03-14-2009, 07:22 AM -
Object access between classes
By qazplm123890 in forum AWT / SwingReplies: 1Last Post: 03-05-2009, 05:00 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks