A variable is a container which holds the value while the Java program is executed. A variable is assigned with a data type.
Variable is a name of memory location. There are three types of variables in java: local, instance and static.
There are two types of data types in Java: primitive and non-primitive.
- Local variable
- It can be used only within the scope where it is declared.
- A local variable must be initialized before you use it.
- A local variable does not have a default value.
- It is created when the method/block is executed and becomes unavailable when the execution leaves that scope.
- A local variable cannot be declared with
static.- A local variable can have the same name as an instance variable (this is called shadowing).
- Local variables are generally stored in the stack frame of the executing method.
- Instance variable
- It is not declared with the
static keyword.- Each object (instance) of the class has its own separate copy of the instance variable.
-Its value can be different for each object.
- It is created when an object is created and normally exists as long as the object exists.
- Instance variables have default values if you don't initialize them.
- Instance variables can be accessed by instance methods.
- They are stored as part of the object in the Heap.
- They can have access modifiers such as
- A variable declared with the
- A static variable is declared inside a class but outside methods, constructors, or blocks.
- It cannot be a local variable.
- There is only one copy of a static variable for the entire class.
- The same static variable is shared by all objects (instances) of the class.
- Changing the static variable through one object affects the value seen by other objects.
- A static variable can be accessed using the class name, without creating an object.
- Static variables have default values if they are not explicitly initialized.
- Static variables are associated with the class, not with individual objects.
- Static variables are initialized when the class is initialized by the JVM.
private, public, or protected.- Static variable
static keyword is called a static variable.- A static variable is declared inside a class but outside methods, constructors, or blocks.
- It cannot be a local variable.
- There is only one copy of a static variable for the entire class.
- The same static variable is shared by all objects (instances) of the class.
- Changing the static variable through one object affects the value seen by other objects.
- A static variable can be accessed using the class name, without creating an object.
- Static variables have default values if they are not explicitly initialized.
- Static variables are associated with the class, not with individual objects.
- Static variables are initialized when the class is initialized by the JVM.
public class VariableExample {
// Static variable
private static int staticVar;
// Instance variable
private int instanceVar;
public void someMethod() {
// Local variable
int localVar = 42;
// Using variables
System.out.println("Static Variable: " + staticVar);
System.out.println("Instance Variable: " + instanceVar);
System.out.println("Local Variable: " + localVar);
}
public static void main(String[] args) {
// Accessing static variable
staticVar = 10;
// Creating an instance of the class
VariableExample obj = new VariableExample();
// Accessing instance variable
obj.instanceVar = 20;
// Calling a method that uses local variable
obj.someMethod();
}
}
Комментариев нет:
Отправить комментарий