クラスには 2 つのコンストラクターがあり、1 つはFile
オブジェクトを受け取り、もう 1 つはオブジェクトを受け取り、キーワードString
を使用したいと考えています。this
実装のある関数はFile
as パラメータを持つもので、String
will callを持つものthis
です。今、コンストラクターで例外をチェックしたいのですが、String
エラーが発生しましたthis
。これは最初の行である必要があります。エラーを確認してから呼び出すにはどうすればよいですかthis
。
これが私のコードです:
public Test (String filename) {
if (filename == null)
throw new NullPointerException("The String you entered is null.");
if (filename.isEmpty())
throw new IllegalArgumentException("The String you entered is empty.");
this(new File(filename)); // error
}
public Test (File f) {
/* implementation here */
}
これは正確なエラーです:Constructor call must be the first statement in a constructor