忍者ブログ
よくわからないことを調べて解説してみるブログ。

2024

0425
×

[PR]上記の広告は3ヶ月以上新規記事投稿のないブログに表示されています。新しい記事を書く事で広告が消えます。

2015

0623
package june20150623;

public class ConstractaDoubleInitializeValue {

	public static void main(String[] args) {
		new Parent();
	}
}

class Human {
	final int age;
	final static int heigt; //	コンパイルエラー!final static 変数は、変数定義時、staticイニシャライザにて初期化が可能。
	final static int weight;//	final static 変数は、変数定義時、staticイニシャライザにて初期化が可能。
	static {
		weight = 50;
	}

	public Human(int age) {
		this.age = age; //	final変数への初期化はコンストラクタでは可能。
	}
}

class Parent extends Human {
	public Parent() {
		this(10);
		age = 20; // コンパイルエラー!final変数への初期化は一度のみ許される。
	}

	public Parent(int age) {
		super(age);
	}
}

class Child extends Human {

	public Child(int age) {
		super(age);
	}
}
実行結果
Exception in thread "main" java.lang.Error: Unresolved compilation problems: 
ブランクの final フィールド heigt は初期化されていない可能性があります
final フィールド Human.age には代入できません
at june20150623.Parent.<init>(ConstractaDoubleInitializeValue.java:12)
at june20150623.ConstractaDoubleInitializeValue.main(ConstractaDoubleInitializeValue.java:6)

拍手[0回]

PR
Post your Comment
Name:
Title:
Font:
Mail:
URL:
Comment:
Pass: Vodafone絵文字 i-mode絵文字 Ezweb絵文字
プロフィール
HN:
たんてーくん
性別:
非公開
フリーエリア
最新CM
[09/25 http://2017.bblbuy.com]
[09/24 http://www.japanform.com]
[09/23 http://www.japanform.com]
[09/22 http://www.japanform.com]
[09/21 http://2017.bblbuy.com]
ブログ内検索
忍者ブログ [PR]
* Template by TMP