The document discusses different types of variables in Java - local, instance/global, and static variables.
Local variables are declared within a method, block or constructor and are only accessible within that scope. Instance/global variables are declared within a class but outside any methods, blocks or constructors and can be accessed throughout the class. Static variables are declared with the static keyword and belong to the class itself rather than any object. They get memory only once in the class area at class loading time.
The document discusses different types of variables in Java - local, instance/global, and static variables.
Local variables are declared within a method, block or constructor and are only accessible within that scope. Instance/global variables are declared within a class but outside any methods, blocks or constructors and can be accessed throughout the class. Static variables are declared with the static keyword and belong to the class itself rather than any object. They get memory only once in the class area at class loading time.
The document discusses different types of variables in Java - local, instance/global, and static variables.
Local variables are declared within a method, block or constructor and are only accessible within that scope. Instance/global variables are declared within a class but outside any methods, blocks or constructors and can be accessed throughout the class. Static variables are declared with the static keyword and belong to the class itself rather than any object. They get memory only once in the class area at class loading time.
- It is user defined name given by user. - Variable can store any type of value. - One variable can store one information at a time and it can be change.
10 int a = 10
Memory
Types of variable a) Local b) Instance/Global c) Static
a) Local variable :- A variable which declare inside method/block/constructor called as local variable Scope of local variable remains only within the method/block/constructor. Local variable is also called temporary variable. Syntax: void add(int a){ int b; Statements ; }
b) Instance / Global variable :- A variable which declare inside the class but outside of all method/block/constructor is called instance variable scope of global variable remains thought the class. global variable is also called permanent variable. Syntax: Class A{ int a; Public static void main(){ Statements; }}
c) Static/ Class variable :- A variable which declare with the help of static keyword is called as static variable. 1. static variable call from same class -->variableName 2. static variable call from diff class--> className.variableName Note: we can access static global variable in both static & non-static method
Program : public class VariableTypes {
static int a = 10; //Instance/ Global variable static int b = 20; //static variable
Static : The static keyword in Java is used for memory management mainly. We can create static methods, variables and blocks. - Static members of class belongs to class itself instead of the Class Object. - Constructor can’t be static.
- If you declare any variable as static, it is known as a static variable. - The static variable can be used to refer to the common property of all objects. - The static variable gets memory only once in the class area at the time of class loading.