Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.
Trending News
Java: I made a class with some int values, now I want to get the objects to have those?
Ok, so I have this Java program, and I have multiple classes and stuff, so I wanted to take the class I made, and make a object with it. But no matter what I do it won't work! It's for that one game I want to make that my last question was about. I tried the following bit of code:
ManaSkills magic=new ManaSkills();
That works fine, but when I do:
int magic.fire blah blah blah, it freaks out! Whether I add =0, or I don't add the =0 part. It won't work! For the code of the class I'm pulling it from:
public class ManaSkills
{
public int fire;//to control fire.
public int water;//to control water.
public int ice;//to control ice.
public int wind;//to control wind.
public int earth;//to control earth.
public int control;//to control another being, used for persuasion.
public int shield;//to make a magic shield.
public int alchemy;//turn items into gold pieces.
public int teleportation;//skill to teleport to towns.
public int generic;//skill for a generic set of magic.
}
I'm not sure what to do. I want to make magic.fire, magic.ice, etc., so my player can have skills, skill levels, and all of the essentials.
I tried "magic.fire=0;". It didn't work.
The code of the class that calls it is:
public class FireManaSkill
{
ManaSkills magic=new ManaSkills();
magic.fire=0;
}
3 Answers
- green meklarLv 79 years agoFavorite Answer
This here:
public class FireManaSkill
{
ManaSkills magic=new ManaSkills();
magic.fire=0;
}
is bad. You are putting these instructions in the class body but not inside a method. This isn't allowed, you cannot modify a property of an object outside a method body. Try putting the appropriate code inside a method, and call it when you need that part of the data to be set.
- 9 years ago
When you create an object of a class, you don't have to declare its member values again for each object. The original declaration in the class is enough.
Instead of
int magic.fire = 0;
Go ahead and use
magic.fire = 0; or whatever.
- husoskiLv 79 years ago
Edit--Important Note: The ManaSkills class must be in a separate file, and must be named "ManaSkills.java" if the class is going to be public. Maybe that's what's causing the error? Test that by commenting-out the "public" part:
/* public */ class ManaSkills
{
... etc.
}
This class won't be visible outside its package until you make it public, but that shouldn't matter for now.
My earlier remarks stand, though. Public visibility for data members is not good OOP design.
- - - - - Earlier message:
What you've described in that class is a list of data items that a ManaSkills object contains. Each holds a value, an int value in each of these cases, but none of them do anything. If you want action, you need to define a method that has the code to implement that action.
If you extend the meaning of "object" to include primitive data items (int, float, char, etc.), then the general rule is "objects are nouns, method are verbs". To get an object to act, you need to call one of its methods.
Normally, an object hides its data with private or protected visibility, instead of public, and implements methods to ask about features of that object or to perform actions that might change the state of the object. The stored data items in the object represent the object's internal state, and normally only the methods of that object are allowed to view or change that directly. (Or, if the visibility is "protected" instead of "private", then methods of a subclass can see those members too. )
I know that you want to get to the game, but you might take some time to develop your skills in a simpler setting. Try working through the Java Tutorials, online at Oracle:
http://docs.oracle.com/javase/tutorial/
Maybe get a book. The old edition of Thinking in Java by Bruce Eckel is available in HTML form for free if you want to see if that book helps. It's on Eckel's "Mindview" website:
http://www.mindviewinc.com/Books/downloads.html
The free edition won't cover generics, but Eckel does a good job of explaining how to design and use objects; and you can learn about generics, enhanced enums, foreach loops, and a few other features online. The meat of how to think in objects is what's important for being effective in Java.