Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I properly declare an instance of a subclass?

I am currently making a text based adventure in Java for the purposes of using it a test platform, to try out new things I learn from this Java book I'm reading.

I am now trying to declare an instance of a subclass (as the player is scripted to find it). The parent class is Item and it has two subclasses: Weapon and Armour.

However, no matter which way I try and declare it in, the IDE I'm using (Eclipse) flags the line with the following error:

No enclosing instance of type Item is accessible. Must qualify the allocation with an enclosing instance of type Item (e.g. x.new A() where x is an instance of Item).

When I attempt to declare it like any of the following:

Item machinePistol = new Weapon(); 
Weapon machinePistol = new Weapon();
Item machinePistol = new Item.Weapon();
Weapon machinePistol = new Item.Weapon();

For reference the item class looks like this:

package JavaAIO;

public class Item 
{
    public String itemName;
    public double itemWeight;

    public class Weapon extends Item
    {
        public double damage;
        public double speed;
    }
    public class Armour extends Item
    {
        public double dmgResist;
        public double attSpdMod;    
    }
}

So if anyone could tell me how I could properly instantiate a Weapon (so I can set the values of its fields and give it to the player), I would greatly appreciate it.

like image 556
James Avatar asked Dec 10 '10 20:12

James


3 Answers

"No enclosing instance of type Item is accessible. Must qualify the allocation with an enclosing instance of type Item (e.g. x.new A() where x is an instance of Item)."

It's pretty self-explaining:

Item machinePistol = new Item().new Weapon(); 

Or:

Item item = new Item();
Item machinePistol = item.new Weapon(); 

However, I strongly recommend to put them in their own classes so that you can end up with:

Item machinePistol = new Weapon();
like image 163
BalusC Avatar answered Nov 09 '22 13:11

BalusC


To do this:

Item machinePistol = new Item();
Weapon machinePistol = new Item.Weapon();

you would want to declare your inner classes as static:

 public static class Weapon extends Item
 {

  public double damage;
  public double speed;

 }

 public static class Armour extends Item
 {
  public double dmgResist;
  public double attSpdMod; 

 }

Or another (and better) option is to not make them inner classes:

public class Item {}
public class Weapon extends Item {}
public class Armour extends Item {}

Then you can make them like this;

Item machinePistol = new Item();
Item machinePistol = new Weapon();
Item someArmour = new Armour();
like image 26
Joel Avatar answered Nov 09 '22 13:11

Joel


You shouldn't declare the classes Weapon and Armour inside Item. Just declare them outside:

public class Item {
    public String itemName;
    public double itemWeight;
}

public class Weapon extends Item {
    public double damage;
    public double speed;
}

public class Armour extends Item {
    public double dmgResist;
    public double attSpdMod;
}

So you can instantiate them like this:

Weapon machinePistol = new Weapon();
like image 1
Hosam Aly Avatar answered Nov 09 '22 13:11

Hosam Aly