then in a different class I have:
I know how to call variables from other classes like I did with my CharClasses class but how does it work when calling from the main class? Also, Im pretty sure this code makes no sense because when I make a new main and call it main2 charpick hasnt happened for this new calling. Is this correct? If so how can I call charpick. Thanks
brent carter wrote:... I know how to call variables from other classes like I did with my CharClasses class but how does it work when calling from the main class? ...
A "main class" is simply a class that contains a main method, which is Java's entry point. Using members (variables or methods) of that class is no different than using members of any other class. Indeed, you can even call the main method directly if you wish, just like any other static method.
brent carter wrote:... Also, Im pretty sure this code makes no sense because when I make a new main and call it main2 charpick hasnt happened for this new calling. Is this correct? ...
That's correct. What's more, charpick is defined locally within the main method, so (unless there is also another "charpick" defined at the class level) main2.charpick has no meaning.
What is your intention in creating a new instance of Main? Do you really want a new object? Or (since you originally asked about accessing members of that main class) did you only do this as an attempt to get to "charpick"?
Generally speaking (as Randall pointed out), a main method should do nothing more than kick things off. For example, create an object and then call some method on that object. From the code you posted, it looks like your instance of "Main" is supposed to represent a player. If that's the case, maybe you want to consider making Player a separate class.
But I think the key to your question is making references to these objects available where they are needed. For example, something like...
Then just use your main method to create an instance of "SomeClass" (maybe it's "Game"?) and kick things off.
For example, console() probably does not belong on Player if Player represents the human player. Consider moving that method to Game. And do as little as possible in Game.main().
If the Player can be a fighter or a mage, perhaps Fighter and Mage should be subclasses of Player, and Game should contain a Player reference variable (not a Mage or a Fighter variable) which you instantiate based on user selection. Something like:
Of course extending Player isn't always the right answer; sometimes having a parent class like that is too constricting. Then you can use interfaces.
If a hippie and an oldlady are both enemies, shouldn't they be classes? If they have similar behaviors -- they can both attack and defend, just in different ways, and perhaps with different strengths -- you could make them extend from the same parent -- say, Enemy -- and have them override certain methods, for example, defend(). The hippie might get its number of defense points by invoking its private defecateOnAPoliceCar() method while the oldlady's defense points are the return value from its private swingHandBag() method.
I think you will want to get rid of the CharClasses class entirely. It isn't at all cohesive. The identities it's describing really need to be manifested as classes. If you went the inheritance route you could get rid of this
and use something like this
which is cleaner.
Another question is, are enemies supposed to be totally different from Players? How many behaviors do they have in common with Players?
So, I haven't even tried to answer your question about the main class. It's more important to get your model sorted out.