staticについてお伺いしたいことがあります。
public class Cleric { String name; static int hp = 50; final int maxHp = 50; static int mp = 10; final int maxMp = 10; Cleric(String name, int hp, int mp){ this.name = name; this.hp = hp; this.mp = mp; } Cleric(String name){ this(name,hp,mp); } Cleric(String name, int hp){ this(name,hp,mp); }
上記のプログラムでhpとmpの宣言でstaticを使わないと2、3個目のコンストラクタでエラーになります。本では自コンストラクタを呼び出すときはthis(引数);と書いてあったのですが、staticを使わないといけないのでしょうか?(今回は重複を極力なくすという問題です)教えていただけると助かります。
回答2件
あなたの回答
tips
プレビュー