Java variable is a name given to a memory location. It is the basic unit of storage in a program.
- The value stored in a variable can be changed during program execution.
- Variables in Java are only a name given to a memory location. All the operations done on the variable affect that memory location.
- In Java, all variables must be declared before use.
Declaring (Creating) Variables
Syntax:
datatype data_name;
or
datatype data_name = value;
or
datatype data_name = value;
- datatype: Type of data that can be stored in this variable.
- variable_name: Name given to the variable.
- value: It is the initial value stored in the variable.
Ex:
int count;
- int : datatype
- cnt : data_name
In this way, a name can only be given to a memory location. It can be assigned values in two ways:
- Variable Initialization
- Assigning value by taking input
Ex:
1 2 3 4 5 6 | // Declaring float variable float simpleInterest; // Declaring and initializing integer variable int time = 10, speed = 20; // Declaring and initializing character variable char var = 'h'; |
Types of Variables
Local Variables
A variable defined within a block or method or constructor is called a local variable.
- These variables are created when the block is entered, or the function is called and destroyed after exiting from the block or when the call returns from the function.
- The scope of these variables exists only within the block in which the variables are declared, i.e., we can access these variables only within that block.
- Initialization of the local variable is mandatory before using it in the defined scope.
Time Complexity of the Method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Auxiliary Space: O(1)
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | public class LocalVariable { public static void main(String[] args) { // x is a local variable int x = 10; // variable String message = "Hello, world!"; System.out.println("x = " + x); System.out.println("message = " + message); if (x > 5) { // local variable String result = "x is greater than 5"; System.out.println(result); } for (int i = 0; i < 3; i++) { String loopMessage = "Iteration " + i; System.out.println(loopMessage); } } } |
Output:
1 2 3 4 5 | message = Hello, world! x is greater than 5 Iteration 0 Iteration 1 Iteration 2 |
Instance Variables
Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block.
- As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
- Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier, then the default access specifier will be used.
- Initialization of an instance variable is not mandatory. Its default value is dependent on the data type of variable. For String it is null, for float it is 0.0f, for int it is 0, for Wrapper classes like Integer it is null, etc.
- Instance variables can be accessed only by creating objects.
- We initialize instance variables using constructors while creating an object. We can also use instance blocks to initialize the instance variables.
Time Complexity of the Method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Auxiliary Space: O(1)
Ex:
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 | // Instance Variables class Main { // Declared Instance Variable public String wrap; public int i; public Integer I; public Main() { // Default Constructor // initializing Instance Variable this.wrap = "AAA"; } // Main Method public static void main(String[] args) { // Object Creation Main name = new Main(); // Displaying O/P System.out.println("Wrap name is: " + name.wrap); System.out.println("Default value for int is " + name.i); // toString() called internally System.out.println("Default value for Integer is " + name.I); } } |
Output:
1 2 3 | Wrap name is: AAA Default value for int is 0 Default value for Integer is null |
Static Variables
Static variables are also known as class variables.
- These variables are declared similarly to instance variables. The difference is that static variables are declared using the static keyword within a class outside of any method, constructor, or block.
- Unlike instance variables, we can only have one copy of a static variable per class, irrespective of how many objects we create.
- Static variables are created at the start of program execution and destroyed automatically when execution ends.
- Initialization of a static variable is not mandatory. Its default value is dependent on the data type of variable. For String it is null, for float it is 0.0f, for int it is 0, for Wrapper classes like Integer it is null, etc.
- If we access a static variable like an instance variable (through an object), the compiler will show a warning message, which won’t halt the program. The compiler will replace the object name with the class name automatically.
- If we access a static variable without the class name, the compiler will automatically append the class name. But for accessing the static variable of a different class, we must mention the class name as 2 different classes might have a static variable with the same name.
- Static variables cannot be declared locally inside an instance method.
- Static blocks can be used to initialize static variables.
Time Complexity of the Method:
Time Complexity: O(1)
Auxiliary Space: O(1)
Auxiliary Space: O(1)
Ex:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | class Main { // Declared static variable public static String wrap = "AAA"; public static void main(String[] args) { // wrap variable can be accessed without object // creation Displaying O/P Main.wrap --> using the // static variable System.out.println("Wrap Name is : " + Main.wrap); // static int c=0; // above line,when uncommented, // will throw an error as static variables cannot be // declared locally. } } |
Output:
1 | Wrap Name is : AAA |