final をつけて宣言した変数は、一度値を代入すると変更できないため、定数のような取り扱いができます。
final の変数名は一般に大文字で書き、単語を連ねる場合はアンダースコアを使います。
クラス | フィールド | 値 |
---|---|---|
java.lang.Integer | public static final int MAX_VALUE | 2147483647 |
java.lang.Math | public static final double PI | 3.141592653589793 |
java.awt.Event | public static final int MOUSE_DOWN | 501 |
class Order { int price; int count; Order( int p, int n ) { price = p; count = n; } int totalPrice() { return price * count; } } class OrderTest { public static void main( String[] args ) { Order ord = new Order( 800, 4 ); System.out.println( "合計"+ ord.totalPrice() +"円" ); } }Order クラスは、コンストラクタに商品の値段と数量を指定してオブジェクトを生成します。
(答えは、次回の Java の Tips で ・・・)
for( int row = 0; row < celldata.length; row++ ) { for( int col = 0; col < celldata[row].length; col++ ) { System.out.println( "["+row+"]["+col+"] = " + celldata[row][col] ); } }col の forループでは、celldata[row].length で、各行の配列要素数が分かります。
int[][] celldata = { { 10, 11, 12, 13, 14 } , { 20, 21, 22 } , { 30, 31, 32, 33 } };