Note that there are some explanatory texts on larger screens.

plurals
  1. PONested static class cannot return its static fields
    text
    copied!<p>I need to create new warrior, assign name, and get his description with function specified in GameCahracter class. When I am trying to run - it stops on <code>weapon.type ; // &lt;&lt;Exception</code> showing that <code>weapon=null</code>. Why? As far as I know warrior constructor assigned to variable <code>weapon</code> a link to new Weapon.Sword. Then using variable weapon I should be able to access it's field <code>type</code>. What is wrong here?</p> <hr> <pre><code>abstract class GameCahracter{ public String name; public String type; public Weapon weapon; public int hitPoints; public String getDescription(){ return name + "; " + type + "; " + hitPoints + " hp; " + weapon.type ; // &lt;&lt; Exception } public static class Warrior extends Player{ public Warrior() { type = "Warrior"; hitPoints = 100; Weapon.Sword weapon = new Weapon.Sword(); } } </code></pre> <hr> <pre><code>abstract class Player extends GameCahracter { } </code></pre> <hr> <pre><code> abstract class Weapon { public int damage; public String type = "default"; public int getDamage(){ return this.damage; } public static class Sword extends Weapon{ public Sword() { String type = "Sword"; int damage = 10; } } } </code></pre> <hr> <pre><code>GameCahracter.Warrior wr = new GameCahracter.Warrior(); wr.setName("Joe"); System.out.println( wr.getDescription()); </code></pre> <hr> <p>EDIT1</p> <p>For some reason I'm having <code>default</code> string when printing out <code>weapon.type</code>. Why? How can I get <code>type</code>to be <code>Sword</code>?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload