A variable is a container that holds the value during the execution of a Java program. A data type is assigned to a variable.The term “variable” refers to the name of a memory region. Local, instance, and static variables are the three types of variables in Java.In Java, data types are divided into two categories: primitive and non-primitive.A variable is a type of named storage that our programs can access. In Java, each variable has a type that governs the memory size and layout, the range of values that may be stored within that memory, and the set of operations that can be applied to the variable.
VARIABLE
A variable is the name of a memory-allocated reserved space. To put it another way, it’s the name of the memory place. It’s made out of the words “variy + ability,” which signifies that its value can be modified.
STATEMENT DECLARING A VARIABLE:
SYNTAX: datatype variable-name=value-to-be-stored;
int c=5;
A PROGRAM USING VARIABLE DECLARATION:
public class Add { public static void main(String[] args) { int a=10; int b=10; int c=a+b; System.out.println(c); } }
Here, data type refers to one of Java’s datatypes, and variable refers to the variable’s name. A comma-separated list can be used to declare many variables of the same type.
can also be performed as:
public class Simple { public static void main(String[] args) { int a,b,c; c=a+b; System.out.println(c); } }
TYPES OF VARIABLES:
- LOCAL VARIABLE
- INSTANCE VARIABLE
- STATIC VARIABLE
LOCAL VARIABLE:
A local variable is a variable declared within the method’s body. This variable can only be used within that method, and the other methods in the class are unaware that it exists.
- The keyword “static” cannot be used to declare a local variable.
- In methods, constructors, or blocks, local variables are declared.
- When a method, function Object() { [native code] }, or block is entered, a local variable is created, and the variable is removed when the method, function Object() { [native code] }, or block is exited.
- For local variables, access modifiers aren’t allowed.
- Only the declared method, function Object() { [native code] }, or block can see local variables.
- Internally, stack-level local variables are implemented.
- Because local variables do not have a default value, they must be declared and given an initial value before being used.
INSTANCE VARIABLE:
An instance variable is a variable declared inside the class but outside the method body. It hasn’t been declared static.
Because its value is instance-specific and not shared across instances, it’s called an instance variable.
- Instance variables are declared outside of any method, function Object() { [native code] }, or block of a class.
- A slot for each instance variable value is created when a space in the heap is allocated for an object.
- When an object is created using the keyword ‘new,’ instance variables are generated, and they are destroyed when the object is destroyed.
- Values that must be referenced by more than one method, function Object() { [native code] }, or block, or critical portions of an object’s state that must be present throughout the class, are stored in instance variables.
- Before or after use, instance variables can be declared at the class level.
- Instance variables can be assigned access modifiers.
- For all methods, the instance variables are visible.
STATIC VARIABLE:
A static variable is one that has been declared as static. It’s not possible to keep it local. You can make a single duplicate of the static variable and share it across all of the class’s instances. Static variables are only allocated memory once, when the class is loaded into memory.
- In a class, but outside of a method, function Object() { [native code] }, or block, the static keyword is used to specify class variables, commonly known as static variables.
- Regardless of how many objects are created from a class, there will only be one copy of each class variable.
- Other than being stated as constants, static variables are rarely used. Variables that are public/private, final, and static are known as constants. Constant variables don’t change their value after they’ve been set.
- The static memory stores static variables. Other than declared final and utilised as either public or private constants, static variables are rarely used.
- When the programme starts, static variables are generated, and when the programme ends, they are destroyed.
- Instance variables are equivalent to visibility. Most static variables, on the other hand, are marked public because they must be accessible to class users.
- Instance variables have the same default values as default values. The default value is 0 for numbers, false for Booleans, and null for object references. Values can be set in the function Object() { [native code] } or during the declaration. Special static initializer blocks can also be used to assign values.
- The class name ClassName can be used to access static variables.
- Variable names (constants) are all in upper case when declaring class variables as public static final. The naming syntax for static variables is the same as for instance and local variables if they are not public and final.
A PROGRAM TO DEMONSTRATE TYPES OF VARIABLES:
public class A { static int a=10;//static variable void method() { int b=20;//local variable } public static void main(String args[]) { int r=15;//instance variable } }//end of class
FINAL VARIABLES
If you don’t want others (or yourself) to alter existing values, you can use the final keyword (which declares the variable as “final” or “constant,” which meaning unchangeable and read-only).If you mark a variable as final, you won’t be allowed to change its value (It will be constant).
final int number = 15; Number = 20; // will generate an error as final variable value cannot be changed.
variables in java|types of variables|simple program demo|