1 / 16

アルゴリズムとプログラミング (Algorithms and Programming)

アルゴリズムとプログラミング (Algorithms and Programming). インスタンス変数とクラス変数 インスタンスメソッドとクラスメソッド ローカル変数とスコープ 変数の寿命、ガーベージコレクション. 第10回:インスタンス変数、クラス変数. 講義資料等:. http://www.pe.titech.ac.jp/~watanabe/lecture/ap/index-j.html. インスタンス変数. class Point { // 点クラス double x; // x 座標 double y; // y 座標 }.

alexa
Download Presentation

アルゴリズムとプログラミング (Algorithms and Programming)

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. アルゴリズムとプログラミング(Algorithms and Programming) • インスタンス変数とクラス変数 • インスタンスメソッドとクラスメソッド • ローカル変数とスコープ • 変数の寿命、ガーベージコレクション 第10回:インスタンス変数、クラス変数 講義資料等: http://www.pe.titech.ac.jp/~watanabe/lecture/ap/index-j.html

  2. インスタンス変数 class Point { // 点クラス double x;// x座標 double y; // y座標 } これまで出てきた変数は、インスタンスごとにメモリ領域が個別に割り当てられ、別々に読み書きができた Point p1 = new Point(); Point p2 = new Point(); インスタンス変数 p1とp2はクラスは同じだが、別のインスタンスであり、それぞれ個別に変数double x, double yを持つ Point: p1 Point: p2 x: 1.5 x: 0.2 y: 2.0 y: 0.0 お互い無関係に 読み書き可能

  3. 実体は1つ クラス変数 同じクラスのどのインスタンスからでも、共通の1つの変数(=メモリ領域)を読み書きしたい場合がある クラス変数 p1からクラス変数の値を 変更すると、p2から見ても 値が変更されている!! Point: p1 Point: p2 x: 1.5 x: 0.2 y: 2.0 y: 0.0 count : count : クラス全体で 共有する変数 count : 2.0 クラス変数

  4. クラス変数の宣言 class Point { // 点クラス double x;// x座標 double y; // y座標 public static int count; } static修飾子 クラス変数へのアクセス方法: オブジェクト変数名.クラス変数名 クラス名.クラス変数名

  5. クラス変数の宣言と利用の例 class Point { // 点クラス double x; // x座標 double y; // y座標 //クラス変数の定義と初期化(static修飾子の有無で出力結果を比較せよ) public static int count = 0; Point( double a, double b ){ x = a; y = b; count++; } } class SampleAP0801 { public static void main(String[] args){ // インスタンスが生成される前からクラス変数は存在する! System.out.println("count=" + Point.count); Point p1 = new Point(1.0,2.0); System.out.println("count=" + p1.count); Point p2 = new Point(3.0,4.0); System.out.println("count=" + p2.count); } } コンストラクタ count=0 count=1 count=2

  6. クラスメソッド クラス単位で共有されるメソッド インスタンス変数にアクセス不可 インスタンスメソッドの呼び出し不可

  7. クラスメソッドは、クラス内で定義されているのでクラスのメンバーといえるが、実体はインスタンスの外に存在するクラスメソッドは、クラス内で定義されているのでクラスのメンバーといえるが、実体はインスタンスの外に存在する オブジェクト(実体) インスタンス1 インスタンス変数 インスタンスメソッド クラス定義 インスタンス2 static クラス変数 クラスメソッド static インスタンス3 アクセス クラスで共有 static static

  8. クラスメソッドの中からインスタンス変数であるx,yを参照することはできない!クラスメソッドの中からインスタンス変数であるx,yを参照することはできない! value:5 value:6 value:7 class Point { // 点クラス double x; // x座標 double y; // y座標 //クラス変数、クラスメソッドの定義 public static int count = 0; public static void printValue(){ System.out.println( "value:" + count ); } public static void printValue(int i){ System.out.println( "value:" + i ); } } class SampleAP0802 { public static void main(String[] args){ //インスタンスが生成される前からクラス変数、クラスメソッドは存在する! Point.count = 5 ; Point.printValue(); Point.printValue(6); Point o = new Point(); o.printValue(7); } } インスタンス変数 クラス変数 クラスメソッド クラスメソッド

  9. main()はクラスメソッドだった! class ユーザ定義 { public static void main(String[] args){ 処理; } } main()メソッドが実行される約束になっている

  10. どこで宣言された変数か?変数の3つの分類 宣言される場所 • インスタンス変数 • クラス変数 • ローカル変数 クラス内 メソッド内

  11. ローカル変数が見える範囲(スコープ)と寿命ローカル変数が見える範囲(スコープ)と寿命 中カッコ{}で囲まれたブロックがスコープの目印 class SampleAP0803 { public static void main(String[] args){ int x = 0; for( int i = 1; i <= 10; i++) { x = x + i; } System.out.println("total = " + x ); } } 変数iの値をこのforブロックの外から参照することはできない 変数xの値はforブロック内からでも参照可能

  12. 変数の寿命 使い終わった変数は破棄され、使用していたメモリが解放される (ガベージコレクション) ローカル変数の寿命:ブロックの最初にメモリが確保され、ブロックの終わりで破棄される インスタンス変数の寿命:newで生成されてから、最後に参照された後、Java VM が判断したタイミング クラス変数の寿命:プログラムの開始から終了まで

  13. バッティングしても有効ローカル変数 SampleAP0804.java class X { static int i = 100; int j = 200; void printValue(){ System.out.println("i= " + i ); System.out.println("j= " + j ); int i = 123; int j = 456; System.out.println("i= " + i ); System.out.println("j= " + j ); } } クラス変数 インスタンス変数 ローカル変数

  14. SampleAP0804.java続き class SampleAP0804 { public static void main(String[] args){ X o = new X(); o.printValue(); } } 実行結果 ローカル変数 クラス変数・インスタンス変数 i= 100 j= 200 i= 123 j= 456 変数名がバッティングした場合は ローカル変数が優先される バッティングしないように名前を付けるのが望ましい

  15. 値を変更しない変数(定数)の宣言 final修飾子 class X { staticfinal int MAX = 100; finalstatic int MIN = -100; final int DEFAULT = 10; } class SampleAP0805 { public static void main(String[] args){ System.out.println("value= " + X.MAX); System.out.println("value= " + X.MIN); X o = new X(); System.out.println( "value= " + o.DEFAULT); } } 値を変更しないのなら、インスタンスごとにこの定数のためにメモリを確保するのは無駄といえる→普通はクラス変数にする(static)

  16. まとめ • インスタンス変数とクラス変数 • インスタンスメソッドとクラスメソッド • ローカル変数とスコープ • 変数の寿命、ガーベージコレクション • final修飾子

More Related