What is a variable?
Variable is the most common term in any programming language. While writing the programs, many a time we need to store data/value in our program. We achieve this by creating variables, Variables are nothing but a memory location or block of memory depending upon the type of data we want to store. We can give some meaningful name to that address and then using the name we access the data multiple times.
Variable Example
int age = 20; // variable "age" holds value 20 as integer
Rules for creating variable names
There are several rules and conventions for naming a variable.
- Variable names are case sensitive.
- Variable names must start with a letter, or the $ or _ character.
- A variable name should not match any java reserved keywords.
Valid Java variable names
int varexample; int varExample; int VAR_EXAMPLE; int _varExample; int $varExample; int varExample1; int varExample_1; int VAR_EXAMPLE_1;
Type of variables
- Local variable
- Instance variable
- Static variable
Local variable
Variables declared in the body of a method are called local variables. This variable can only be used within the method, no other methods are allowed to access it.
Example
public void doSomething() { // Local variables inside the method int x = 10; int y = 20; int sum = x + y; }
Additional points
- Local variables are declared in methods, constructors, or blocks.
- Access modifiers cannot be used for local variables.
- Local variables are visible only within the declared method, constructor or block.
- Local variables are implemented at the stack level internally.
- Local variables must be initialized before use.
Instance variable
Instance variables are declared inside a class but outside of any method, constructor or block. As memory is allocated for objects in the heap, slots are created for each instance variable value. The instance variable is also known as field or property of an object.
Example
public class Person { String name; // instance variable int age; }
Addition points
- Instance variables are declared in a class, but outside a method, constructor or any block.
- When memory is allocated for an object in the heap a slot for each instance variable value is created.
- Instance variables are created when an object is created with the use of the keyword ‘new’ and destroyed when the object is destroyed.
- Instance variables can be declared in the class level before or after use.
- Access modifiers can be given for instance variables.
Static variable
Java static variables belong to classes and are initialized only once at the start of execution. Static variables can be accessed directly by class name and no object is required to access it.
Example
public class Person { static int countryCode = 91; // static variable }
Additional points
- Static means to remain constant.
- Static variables are also known as a class variable.
- Static variables are created when the program starts and destroyed when the program stops.
- Static variable need not be called from an object.
- Static variables can be accessed by calling with the class name ClassName.VariableName.