Java Class Member Variables
Member Variables
A member variable is a variable declared outside a method. A variable declared inside a method is called a local variable.
- Member variables
- Object variables
- Object attribute variables
- Object reference variables
- Class variables
- final variables
- Object variables
Structure of Member Variables
Except for access modifiers such as public, private, and protected, member variable declarations have the same structure as ordinary variables.
[public/private/protected] [static/final] variableType variableName;
Declaring Member Variables
Except for access modifiers, declaring a member variable is the same as declaring an ordinary variable.
int a;
public int b;
private MyClass myclass;
Declaration, initialization, and creation can be done together, as shown below.
public int b = 0;
private MyClass myclass = new MyClass();
By convention, member variable names start with a lowercase letter, just like ordinary variable names. However, variables declared as static final are often written entirely in uppercase.
private static final int MY_NUM;
You can think of this as playing the role of a constant.
Access Control Modifiers
-
public- Accessible from all classes.
-
private- Usable only inside the declared class.
-
protected- Accessible only from the same class or subclasses derived from that class.
-
No access modifier
- A member variable declared without a modifier can be accessed only from classes in the same package.
Object Variables
Object variables represent characteristics that an object can have. Depending on the value represented by the object variable, they are divided into two forms: object attribute variables and object reference variables.
Object Attribute Variables
Object attribute variables are variables that hold values of primitive data types such as int, float, char, and byte.
public int b;
The value of the variable is copied and passed.
Object Reference Variables
Object reference variables are variables that point to created objects. They hold a reference to an object, that is, the address where the created object is stored.
private MyClass myclass;
Because the address of the object is copied and passed, it ultimately points to the same object.
Class Variables - static Keyword
Class variables are also called static variables. They are declared with static and have the concept of global variables.
- Declared with
static, with the concept of global variables. - All objects created from the class share one class variable. Class variables can be used for communication between objects created from one class or to represent common attributes among those objects.
- Unlike ordinary variables, they cannot be accessed through an object name. They are accessed through the class name.
final Variables
- When a variable is specified with
final, it has a constant value that cannot change. - A variable with
finalcan be initialized only once, and its value cannot be changed afterward. - By naming convention, final variables are often written in uppercase.