A variable in java is the name given to a memory location, where a program can store data. Each variable in java is of a specific data type, determines the values it can store,
how much memory is required to store this data and the operations that can be performed on it.
In other words, a variable is a name of a memory location. A program can store and manipulate the value stored using variable's identifier.
Rules for Writing Variable Name in Java
Following are the rules for writing names of a variable.
- Variable names in java are case-sensitive. For Example, "Sum" and "sum" will be considered as two different variables.
public class CaseSensitiveVariables { public static void main(String args[]) { int sum = 100; int Sum = 150; System.out.println("sum : " + sum); System.out.println("Sum : " + Sum); } }Output
sum : 100 Sum : 150
int sum; // Valid variable name. int _sum; // Valid variable name but not recommended. int $sum; // Valid variable name but not recommended. int 1sum; // InValid variable name.
int su m; // InValid variable name. int sum'; // InValid variable name.
Declaration of Variables in Java
Variables in java must be declared before their use in a program. Declaration of a variable associate it with a data type. Here is the syntax for declaring a variable in java :
data_type variable_name;
data_type is one of the data types in java and variable_name is name of variable. variable_name can be any valid identifier.
For Example :int sum;
We can also initialize a variable at the time of declaration by assigning an initial value.
int sum = 10;
Multiple variables of same data type can be declared by separating the variable names by comma.
int sum, amount, interest; int count = 100, total = 40;
Types Of Variables in Java
Java programming language defines three types of variables:
Local Variables
- A local variable is visible only inside their function, only statements inside function can access that local variable.
- Local variables are declared when control enters a function and local variables gets destroyed, when control exits from function.
- Local variables store temporary state inside a method.
- Access modifiers are not used for local variables.
- Local variables are stored n stack.
Instance Variables
- Instance variables are declared in a class, but outside of any class's methods and constructors.
- Every object of a class will have it's own instance variables.
- Access modifiers are used for instance variables.
- Instance variables get created when a new object is created using new operator and it gets destroyed when object gets destroyed.
- Instance variables are accessibly by all member methods and constructor of a class.
- Instance variables have default values. It is zero for numerical variables, false of boolean variables and null for object references.
Class/Static Variables
- A class variable is any field declared with the static modifier.
- Only one copy of this variable is created for a class, irrespective of the number of objects created. If we created 10 objects of class C, then all 10 onjects will share same copy of static variable. There will be only one copy of static variable not 10 copies(one for each object).
- Static variables are stored in the static memory. Static variables are created only once at the time class loading.
- Static variables are also known as class variables, as they are associated with a class unlike instance variables which are specific to objects. Static variables can be accessed by calling with the class name ClassName.StaticVariableName. We don't have to create any object to access static variables.
Best Practices for Variable Usage
- Meaningful Variable Names : Choose variable names that convey the purpose or meaning of the data they represent. This enhances code readability.
- Initialize Variables Before Use : Always initialize variables before using them to avoid unexpected behavior. Uninitialized variables may contain garbage values.
- Avoid Magic Numbers : Avoid using "magic numbers" (hard-coded numerical values) in your code. Instead, assign them to named constants for better maintainability.
- Use final for Variables That Shouldn't Change : Declare variables as final if their values should not be modified after initialization. This improves code safety and readability.
Conclusion: Mastering Variables in Java
In Java, variables are the backbone of your code, facilitating the storage and manipulation of data. By understanding variable declaration, initialization, data types, and scope, you can write more efficient and readable programs. Adhering to best practices, such as meaningful variable names and proper scoping, further contributes to the clarity and maintainability of your code.
As you continue your journey in Java programming, practice using variables in various scenarios. Experiment with different data types, explore scoping rules, and embrace the principles of clean and effective coding. Mastering variables is a key step towards becoming a proficient Java developer.