0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【Java入門】staticメソッドからインスタンス変数にアクセスできないのはなぜ?

0
Posted at

この記事の対象読者

  • Java を学び始めた方
  • main メソッドから変数を呼んでコンパイルエラーになった方

起きたこと

main メソッドからフィールドを参照したらエラーになりました。

public class learnEnum {
    public static void main(String[] args) {
        System.out.println(myTime); // ← コンパイルエラー                                                      
    }                                                                                                          
                                                                                                               
    private LocalDateTime myTime = LocalDateTime.now();                                                        
}               

non-static variable myTime cannot be referenced from a static context
                                                                                                               
原因
                                                                                                               
main は static メソッド=クラスに属するので、オブジェクトを作らなくても動きます。                              
一方 myTime はインスタンス変数=オブジェクトに属するので、new するまで存在しません。
                                                                                                               
存在しないものは参照できない、というシンプルな理由です。                                                       
                                                                                                               
解決方法                                                                                                       
                
方法1: フィールドも static にする

private static LocalDateTime myTime = LocalDateTime.now();

クラスに1つだけ存在すればいい値ならこれが簡単です。                                                            
 
方法2: インスタンスを作ってアクセスする                                                                        
                
public static void main(String[] args) {
    learnEnum obj = new learnEnum();                                                                           
    System.out.println(obj.myTime);
}                                                                                                              
                
オブジェクトごとに異なる値を持たせたい場合はこちら。                                                           
 
使い分けの目安                                                                                                 
                
┌────────────────────────────────┬────────────────────────┐                                                    
│             ケース             │          選択          │
├────────────────────────────────┼────────────────────────┤
│ 定数・共通設定値               │ static フィールド      │
├────────────────────────────────┼────────────────────────┤
│ オブジェクトごとに異なるデータ │ インスタンス変数 + new │                                                    
└────────────────────────────────┴────────────────────────┘                                                    
                                                                                                               
参考リンク                                                                                                     
                
- https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html                                           
 
---                         
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?