File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Calling a variable form the main class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Calling a variable form the main class" Watch "Calling a variable form the main class" New topic
Author

Calling a variable form the main class

brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
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
Bartender

Joined: May 31, 2011
Posts: 1776
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

Joined: Oct 21, 2000
Posts: 4339
    
    2

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


SCJP
Visit my download page
brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
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

Joined: Dec 15, 2011
Posts: 34
im sorry randall I don't understand. You could simplify it for me?
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
You need to store enemycounter in a variable that has class scope, then it can be reached from the attackmenu method.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
thanks marc for the great post. Im looking at it and working things out.
brent carter
Ranch Hand

Joined: Dec 15, 2011
Posts: 34
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

Joined: Oct 21, 2000
Posts: 4339
    
    2

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

Joined: Jan 20, 2008
Posts: 137
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.


Costs matter. Justice lies in processes not outcomes. Crime is caused by criminals.
Thomas Kennedy
Ranch Hand

Joined: Jan 20, 2008
Posts: 137
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

Joined: Dec 15, 2011
Posts: 34
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.
 
jQuery in Action, 2nd edition
 
subject: Calling a variable form the main class
 
Similar Threads
problem in static
Main method inheritance
Variables from another class won't assign to main class
Help me out with the simple calculator code Please!!!!
Incompatible operand types Scanner and int