Results 1 to 5 of 5

Thread: Help please

  1. #1
    damo190 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Question Help please

    ok so I have made a class called Monster. This class will be extended by all of the monster classes in my text adventure. I then have a class file which will declare the basic parts of the monster this is done by firstly declaring the Monster for example like:

    Monster MONSTERNAME

    MONSTERNAME = MONSTERCLASS(PARAMETERS WHICH DECLARE THE MONSTERS ATTACK DEFENSE HEALTH ETC. );

    is there a way to declare MONSTERNAME by adding public static

    to make it

    public static Monster MONSTERNAME

    MONSTERNAME = MONSTERCLASS(PARAMETERS WHICH DECLARE THE MONSTERS ATTACK DEFENSE HEALTH ETC. );

    Although when I try to do this it says that only final is permitted.
    There must be something I must add to the class Monster.
    Could someone please tell me what I must add to the class Monster so the public static is permitted.

  2. #2
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    983
    Rep Power
    2

    Default Re: Help please

    You can't just make Monster have static fields without knowing what it means. Making it static because you want to access it from public static void main(String[] args) is wrong. Do you know what static does? If yes, why should a monster name be static? Hint: static is shared among all instances.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,407
    Rep Power
    20

    Default Re: Help please

    Please go through the Forum Rules, particularly the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    damo190 is offline Member
    Join Date
    Jun 2013
    Posts
    3
    Rep Power
    0

    Default Re: Help please

    I do know what static is and the reason I want to access it in static form is because I would like to access this variable through out different classes and the only way i know to access them like that is statically. Also monster is NOT THE MAIN CLASS. I can only define it as static if it is an interface and there can't be constructors in interfaces. Please I need help

  5. #5
    SurfMan's Avatar
    SurfMan is online now Godlike
    Join Date
    Nov 2012
    Location
    The Netherlands
    Posts
    983
    Rep Power
    2

    Default Re: Help please

    If you want to access objects that are inside other objects you should provide getters for it:
    Java Code:
    public class MyTextAdventure {
        private Monster theBigScaryMonster;
    
        public MyTextAventure() {
           theBigScaryMonster = new Godzilla();
        }
    
        public Monster getMonster() {
            return theBigScaryMonster;
        }
    }
    From the object (which is an instance of MyTextAdventure) you can now say
    Java Code:
    myTextAdventure.getMonster();
    If the monster is not directly accessible, but in some collection, here's an example of how to do that:

    Java Code:
    public class MyTextAdventure {
        private Map<String, Monster> monsters;
    
        public MyTextAventure() {
            monsters = new HashMap<>();
            addMonster( new Dinosaur("Godzilla"));
        }
    
        public Monster getMonster(String name) {
            //feel free to throw an IllegalArgumentException when the name does not exist.
            //Otherwise this method will return null if the monster does not exist.
            return monsters.get(name);
        }
    
        public void addMonster(Monster m) {
            monsters.put(m.getName(), m);
        }
    }

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
  •