Java プログラムのインスタンスは、クラスまたは関数でオブジェクトが呼び出されるたびに作成されます。オブジェクトが呼び出されるたびにメモリ記憶ユニットを割り当てる目的で変数が使用され、インスタンスが作成されます。インスタンスに対して実行された変更や操作は、インスタンス変数と対応するメモリ ユニットに自動的に反映されます。このタイプの変数は、いくつかのデータ型 (int、char、long、short、boolean、float、double、byte、object) でサポートされています。
構文:
無料ソフトウェア開発コースを始めましょう
Web 開発、プログラミング言語、ソフトウェア テスト、その他
1 2 | <datatype> <variable_name>;
<datatype> <variable_name> = <initializing_value>;
|
ログイン後にコピー
変数には主に 3 種類があります:
Java のインスタンス変数
インスタンス変数はクラス内で宣言/定義されますが、コンストラクター、メソッド、ブロックの外にあります。インスタンス変数は、インスタンス (オブジェクト) 固有であり、インスタンス (オブジェクト) 間で共有されず、特定のインスタンスの変数に加えられた変更は他のインスタンスに反映されないため、そのように呼ばれます。ヒープ内のオブジェクトにメモリが割り当てられると、各インスタンス変数のスロットが作成されます。オブジェクトのインスタンス変数は、オブジェクトが「new」で作成されるときに作成され、オブジェクトが破棄されるときに破棄されます。オブジェクトは状態を保存するためにそれらを使用します。
構文:
1 2 3 4 5 6 | public class Employee {
public String Name;
private int salary ;
public static String company;
}
|
ログイン後にコピー
インスタンス変数の宣言
インスタンス変数にはデフォルト値があります。したがって、ローカル変数とは異なり、初期化せずに宣言できます。変数のデフォルト値は、そのデータ型によって異なります。
Datatype |
Default Value |
boolean |
false |
byte |
0 |
short |
0 |
int |
0 |
long |
0L |
char |
u0000 |
float |
0.0f |
double |
0.0d |
Object |
null |
データ型
デフォルト値
ブール値
false
バイト
0
短い
0
int
0
長い
0L
文字
u0000
float
0.0f
double
0.0d
オブジェクト
null
テーブル>
Characteristics of Instance Variable in Java
- Instance variables are visible to the constructor, method(s) and block(s). It is usually recommended to declare them as private, but the users can change the level of visibility by declaring it with various access modifiers suitable for their program.
- The variable name generally calls instance variables, but when these variables are used within a static method, they should be called using a fully specified name with the variable’s reference.
Syntax:
1 | Objectreference.variable_name
|
ログイン後にコピー
- Instance variables of a base class can be used by the subclasses depending on the access level given to the variable.
- Instance variables can be declared final to protect the constness of a declared variable. final instance variables are initialized while declaration and can’t be modified. They can also be declared and initialized in block and constructor.
- The instance variable can use the default access modifier.
- Instance variables can be declared. If a variable is declared as transient, it is not serialized into a byte stream. Its state changes are not stored. In deserialization, when the object is created back from the byte stream, transient variables hold their respective data type’s default values.
- Instance variables cannot have abstract, synchronized, strictfp or native modifiers as these are applicable to methods only.
- Instance variables cannot be declared static if declared, they become class level variables.
Examples of Instance Variable in Java
Examples of the instance variable in java are given below:
Example #1
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | import java.io.*;
public class Employee {
public String Name;
private int salary;
public static String company;
public Employee (String eName) {
Name = eName;
}
public void setSalary(int eSal) {
salary = eSal;
}
public void printEmp() {
System.out.println( "name : " + Name );
System.out.println( "salary :" + salary);
System.out.println( "Company :" + Employee.company);
}
public static void main(String args[]) {
Employee.company = "Google" ;
Employee employee_one = new Employee( "Jack" );
employee_one .setSalary(100000);
employee_one .printEmp();
Employee employee_two = new Employee( "Jill" );
employee_two .setSalary(200000);
employee_two .printEmp();
}
}
|
ログイン後にコピー
Output:

In the above example, we can see the difference between instance variables and static variables. We have created two objects for employees. The name and salary of the objects are different, but the company is the same for both. This is because it is a static variable and the rest are instance variables.
By changing the line “private int salary; “ in the above code to “ private final int salary = 500;”. We’re changing the behavior of the variable. Since we’re trying to modify a final variable, the compiler throws the following error.
Output:

Example #2
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | import java.io.*;
public class DefaultValues {
boolean val1;
double val2;
float val3;
int val4;
long val5;
String val6;
public static void main(String[] args) {
System.out.println( "Default values are :" );
DefaultValues d = new DefaultValues();
System.out.println( "Val1 = " + d.val1);
System.out.println( "Val2 = " + d.val2);
System.out.println( "Val3 = " + d.val3);
System.out.println( "Val4 = " + d.val4);
System.out.println( "Val5 = " + d.val5);
System.out.println( "Val6 = " + d.val6);
}
}
|
ログイン後にコピー
The above code snippet on execution results in the below output. As mentioned earlier, instance variables have default values if they are not initialized while declaring. This program prints the default values of all the data types.
Output:

Example #3
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | import java.io.*;
public class Test implements Serializable
{
int val1 = 10;
int val2 = 20;
transient int val3 = 30;
transient static int val4 = 40;
transient final int val5 = 50;
public static void main(String[] args) throws Exception
{
Test input = new Test();
FileOutputStream fos = new FileOutputStream( "abc.txt" );
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(input);
FileInputStream fis = new FileInputStream( "abc.txt" );
ObjectInputStream ois = new ObjectInputStream(fis);
Test output = (Test)ois.readObject();
System.out.println( "val1 = " + output.val1);
System.out.println( "val2 = " + output.val2);
System.out.println( "val3 = " + output.val3);
System.out.println( "val4 = " + output.val4);
System.out.println( "va15 = " + output.val5);
}
}
|
ログイン後にコピー
Executing the above program results in the below-given output. variable val3 is declared transient, so its value was not stored into the byte stream when the Test object was serialized.whereas val4 and val5 are not instance variables because they violate the rules of instance variables. Hence their states remain unaffected.
Output:

以上がJavaのインスタンス変数の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。