简体   繁体   中英

Accessing data from another class Java

Okay, this is total noob stuff, and this is a simple concept I just cannot grasp from everything I've looked at.

Currently I have some a GUI with a JList, when you select the 1st, 2nd, 3rd or whatever thing in the list it gets the respective value from a list. However, I want to move this list to a 'data' class (or all my values are elsewhere, for easier maintainence - and yes I want them hard coded into the software).

new DefaultComboBoxModel(
                        new String[] { "A", "B", "C", "D", "E" });
                        final Double[] letterCost = { 2.5, 1, 1, 1, 2.5 };

so when someone clicks E, for example, in the JList letterCost is set to 2.5 I want to move this bit

final Double[] letterCost = { 2.5, 1, 1, 1, 2.5 };

into another class. I'm over simplifying what I'm actually doing. I just want to get my head around the concept. I've never worked with multiple classes before.

So how would I go about making a new class, having the data in that class, and have it accessed as it currently would be from my original class?

Thanks..

oh, and if I had something that changed the values (say every time someone picked a letter the value increased by 1), would that continue to work the same?

You need a reference to that class, which may either be a variable you get from the method parameters, or an instance variable in your new class. Then you use (name of the reference).letterCost.

I really suggest reading some introductory books like Head First Java.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM