Results 1 to 10 of 10
Thread: Problem with class Object
- 07-24-2010, 01:40 PM #1
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
[SOLVED] Problem with class Object
G'day guys,
I have a class setup(ill link all these below and highlight the points im referencing), and after creating an object from that class im having a hell of a time to get the variable names passing to the object.
Example:
I have a class of ''creature'', with the stats strength, defence etc
when i create a new object of that creature(in this case Enemy), its not able to set the statistics of Strength etc. so Enemy.Strength is not valid.
Ive tried get/sets but i must be making a mistake.
As i said, im linking the code below, but ive tried so many different options atm, i wanted to list out what id done first lol.
**CLASS**
**OBJECT CODE**Java Code:/* * To change this template, choose Tools | Templates * and open the template in the editor. */ package gamewithoutaname; /** * * @author Marak */ public class skeleton { //public Bicycle(int startCadence, int startSpeed, int startGear) // trying to dec the variables locally to make it easier to pass on the values\ public static String Name = "Skelton"; public static int Strength; public static int Speed = 2; public static int Defence = 4; public static int Hp = 2; public static int Level = 1; public static int Experience = 0; // NOT FREAKING PASSING! public skeleton(int StartStrength){ Strength = StartStrength; } public void setStrength(int newStrength) { Strength = newStrength; } public void setStr(int Str){ this.Strength = Str; } }
As you can see (probably) my code gets messier each time i try something new(notibly the setSTR etc).Java Code:public void NewCreature(){ Object Enemy = new skeleton(1); Enemy.setStrength(5); }
Ive been at this for a few hours now, each time ive made a little more progress, but its been stuck here for a while, so i've bitten the bullet and decided to ask for help lol
-= marak
EDIT:
Ignore my comments in my code btw :P Thats the stress getting to me hahaLast edited by marak; 07-24-2010 at 02:30 PM. Reason: It was solved!
-
One major problem you have is your overuse of statics. If Creature has a static variable (note that class names by convention should be capitalized and variable names not), then if you set the variable for one Creature object, you change it for all Creature object. Much better is to use instance fields that can be set individually for each Creature object. Better still, I urge you to get rid of everything static in your program other than the main method.
- 07-24-2010, 02:10 PM #3
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
Thanks for the quick reply, I had those set for when i was directly passing variables to another area(i should have remembered to move them). Ill change those over, and try again with my set/gets.
Thanks mate,
Marak
EDIT:
OK ive changed all those over, adjusted the file name, changed the references internally, im still having the issue with this part:
Now i know im missing something, as ive been having trouble with the class's and objects all night. The problem is the Enemy.setStrength(5); is kicking up an error with the .setStrength - infact there's no auto-complete options if i hover over the Enemy. - its as if its not loading a new object correctly from Skeleton.Java Code:public void NewCreature(){ Object Enemy = new Skeleton(1); Enemy.setStrength(5); }
Skeleton Code
I know there's two different setStrength and setSTR, one's from earlier testing to get it to work lol.Java Code:public class Skeleton { //public Bicycle(int startCadence, int startSpeed, int startGear) // trying to dec the variables locally to make it easier to pass on the values\ public String name = "Skelton"; public int strength; public int speed = 2; public int defence = 4; public int hp = 2; public int level = 1; public int experience = 0; // NOT FREAKING PASSING! public Skeleton(int StartStrength){ strength = StartStrength; } public void setStrength(int newStrength) { strength = newStrength; } public void setStr(int Str){ this.strength = Str; }Last edited by marak; 07-24-2010 at 02:16 PM.
- 07-24-2010, 02:21 PM #4
One observation. This line of code:
creates an object that goes out of scope when the newCreature() method exits. (lowercase first letter for methods)Java Code:Object Enemy = new Skeleton(1);
If you want to use it later, the method should return it.
What does this mean?kicking up an errorLast edited by Norm; 07-24-2010 at 02:23 PM.
- 07-24-2010, 02:23 PM #5
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
Thanks Norm, googling a few helps for that, and ill see how she go's!
- Marak
-
It seems to work for me now:
Java Code:public class SkeletonTest { public static void main(String[] args) { Skeleton skel = new Skeleton(5); System.out.println("Strength is: " + skel.strength); skel.setStrength(10); System.out.println("Strength now is: " + skel.strength); } } class Skeleton { public String name = "Skelton"; public int strength; public int speed = 2; public int defence = 4; public int hp = 2; public int level = 1; public int experience = 0; // NOT FREAKING PASSING! public Skeleton(int StartStrength) { strength = StartStrength; } public void setStrength(int newStrength) { strength = newStrength; } }
-
Also, why is enemy an Object type variable and not a Skeleton type variable? That's your current problem since Object doesn't have a setStrength method.
- 07-24-2010, 02:29 PM #8
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
THATS IT Furbarable!
The problem was i had object before Enemy = new skeleton
I mis-takenly believed you needed object there to point out its a new object, where as there's two references to the object base(in this case Skeleton).
Thats fixed the problem tyvm.
Now ive changed that Enemy. works, all the skeleton variables are coming up and i can start setting them in code(and calling from elsewhere).
Thanks both of you for your help, i can get back to work on it now!\
- marak
EDIT: and for anyone else reading - i should have pointed out the values in Skeleton were only meant to be temporary(its the variable names im passing to new objects of Skeleton for multiple objects, they were originally only to test to pass the values before i started creating new instances of the objects.)
- 07-24-2010, 03:07 PM #9
Your code brings up the importance of nameing conventions:
When I quickly scanned the above, I saw a call to a static method in the class: Enemy.Java Code:Enemy.setStrength(5);
If it had been:
I would have looked for the type of the variable: enemy to see if it were used properly.Java Code:enemy.setStrength(5);
- 07-24-2010, 03:40 PM #10
Member
- Join Date
- Jul 2010
- Posts
- 5
- Rep Power
- 0
Your right, it drums in a little more what my programming teacher says lol. Ill have to be a lot more stringent with myself :)
Thanks again for the tips guys, it was the major stepping stone, now my missus is looking foward to playing around with it tomorrow(and she's glad im not grumpy at the computer anymore haha.)
Similar Threads
-
Create object of unknown class, based on existing object
By Zack in forum New To JavaReplies: 2Last Post: 06-22-2010, 04:29 AM -
add object to ArrayList (object is from extends other class)
By mBull in forum Java AppletsReplies: 3Last Post: 03-15-2010, 08:44 PM -
object vs class
By billq in forum New To JavaReplies: 5Last Post: 12-24-2009, 04:56 PM -
Getting name of object class
By Java Tip in forum Java TipReplies: 0Last Post: 11-05-2007, 05:22 PM -
Creating object of Type Object class
By venkatv in forum New To JavaReplies: 3Last Post: 07-17-2007, 03:33 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks