Results 1 to 10 of 10
  1. #1
    marak is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Unhappy [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**
    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;
        }
    
    }
    **OBJECT CODE**

    Java Code:
     public void NewCreature(){
    
            Object Enemy = new skeleton(1);
            Enemy.setStrength(5);
    
        }
    As you can see (probably) my code gets messier each time i try something new(notibly the setSTR etc).

    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 haha
    Last edited by marak; 07-24-2010 at 02:30 PM. Reason: It was solved!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  3. #3
    marak is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    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:

    Java Code:
       public void NewCreature(){
    
           Object Enemy = new Skeleton(1);
           Enemy.setStrength(5);
    
        }
    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.

    Skeleton Code
    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;
        }
    I know there's two different setStrength and setSTR, one's from earlier testing to get it to work lol.
    Last edited by marak; 07-24-2010 at 02:16 PM.

  4. #4
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    One observation. This line of code:
    Java Code:
    Object Enemy = new Skeleton(1);
    creates an object that goes out of scope when the newCreature() method exits. (lowercase first letter for methods)
    If you want to use it later, the method should return it.

    kicking up an error
    What does this mean?
    Last edited by Norm; 07-24-2010 at 02:23 PM.

  5. #5
    marak is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    Thanks Norm, googling a few helps for that, and ill see how she go's!

    - Marak

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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;
       }
    
    }

  7. #7
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    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.

  8. #8
    marak is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    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.)

  9. #9
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,548
    Rep Power
    23

    Default

    Your code brings up the importance of nameing conventions:
    Java Code:
     Enemy.setStrength(5);
    When I quickly scanned the above, I saw a call to a static method in the class: Enemy.
    If it had been:
    Java Code:
     enemy.setStrength(5);
    I would have looked for the type of the variable: enemy to see if it were used properly.

  10. #10
    marak is offline Member
    Join Date
    Jul 2010
    Posts
    5
    Rep Power
    0

    Default

    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

  1. Replies: 2
    Last Post: 06-22-2010, 04:29 AM
  2. Replies: 3
    Last Post: 03-15-2010, 08:44 PM
  3. object vs class
    By billq in forum New To Java
    Replies: 5
    Last Post: 12-24-2009, 04:56 PM
  4. Getting name of object class
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-05-2007, 05:22 PM
  5. Creating object of Type Object class
    By venkatv in forum New To Java
    Replies: 3
    Last Post: 07-17-2007, 03:33 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •