• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling a variable form the main class

 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone. In my main class I have the following code:



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
 
John Jai
Rancher
Posts: 1776
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How do you run your classes? What does the class Main contains? Does the other class you mentioned has a main() method to run?
 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you are going about it in the wrong way. main() should just create an instance of you class, and that is all.

public class Game
{
public static void main(String args[])
{
Game test = new Game();
}
}
all the rest of the code should be in the Game class constructor and methods
 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is only one class with the main() function and that is the main class. Here is an even simpler version of the question I am having between to parts of a class: In line 51 how do I call that enemycounter variable from the battle section(?) above without running the whole process again.

 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
im sorry randall I don't understand. You could simplify it for me?
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to store enemycounter in a variable that has class scope, then it can be reached from the attackmenu method.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks marc for the great post. Im looking at it and working things out.
 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey guys. I am going to start school next semester for compsci, and this programming is me getting comfortable with JAVA because the last time I programmed was on QBASIC many years ago. Here is my program so far and please go to town on it and tell me anything that is amiss. The program runs well thanks to the corrections from above. Is this what yall were talking about?

 
Randall Twede
Ranch Hand
Posts: 4469
3
Java Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it works now and it looks better(like java code). maybe someone more hyper and zealous will give you more feedback. me, im an old sot in search of another beer
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you haven't already done so I suggest taking pencil and paper and drawing your classes in UML-like fashion, as a little table with the classname at the top in its own row and all the methods and members below, with + and - to indicate public or private, and methods() shown with parentheses. When you have them all jotted down look them over, and consider whether the various pieces are in the right spot.

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.
 
Thomas Kennedy
Ranch Hand
Posts: 137
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Think over these Enemy things. You've got an Enemy class but you're not doing a lot with it. And does it sound right to have the Enemy class's battle() method instantiating an Enemy? When a battle starts, shouldn't the enemy already exist?

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.


 
brent carter
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas thanks for the great reply. My code got out of control because as i slowly learned more things i wanted to test them so my code is more like a line oriented code than object oriented. I am going to use your template as a base to learn about those things you talked about because I have not learned about them yet. Thanks again.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic