Variables in Java
Following are the types of Variable in Java -
Variables in Java
Following are the types of Variable in Java -
- Static Variable
- Non-Static/Instance/Object Variable
- Local Variable
- Reference Variable
Static Variable
- Static Variables are declared outside all the methods but inside a class using a static keyword.
- Static Variables can be used anywhere in the class.
- It is not mandatory to initialize static variables, if we don’t initialize it automatically gets some default value zero, from the compiler. Example:- static int i; get default value zero (0) and for float default value should be 0.0
- It is not mandatory to use the Class Name while accessing the static variable, if we do not use the class name, then the compiler will automatically append the class name.
- When we access static variables with a reference we will get a warning message. Warning messages will not halt your program. And hence we can still run the program.
Following are some examples of Static Variable -
Example 1:
class A{
static int i =10;
public static void main(String [] args){
System.out.println(A.i);
A a1 = new A();
a1.test();
}
public void test(){
System.out.println(A.i);
}
}
Output:-
10
10
Example 2:
class B{
static int i;
public static void main(String [] args){
System.out.println(i);
}
}
Output:-
0 because it will get zero as the default value.
Example 3:
class C{
static int i;
public static void main(String [] args){
C c1 = new C();
System.out.println(c1.i);
}
}
Output:-
0 because it will get zero as the default value. And we will get a warning message.
Non-static Variable
- To access non-static variables, object creation is a must, it’s mandatory.
- Non-static variables are declared outside all the methods, but inside the class without the static keyword.
- Non-static variables can be accessed only after object creation.
- It is not mandatory to initialize the non-static variable, if we don’t initialize it automatically gets some default value from the compiler.
Following are the examples of Non-static Variables:-
Example 1:
class A{
int i = 10;
public static void main(String [] args){
A a1 = new A();
System.out.println(a1.i);
}
}
Output:-
10
Example 2:
class B{
int i = 10;
public static void main(String [] args){
System.out.println(B.i); //Error
}
}
Output:-
It will throw an error because we can’t access non-static variables by className we have to initialize an object.
Example 3:
class C{
int i ;
public static void main(String [] args){
C c1 = new C();
System.out.println(c1.i);
}
}
Output:-
0 It is not mandatory to initialize the non-static variable, if we don’t initialize it automatically gets some default value from the compiler. it gets a default value of zero
Local Variable
- Local variables are created within the method and accessed within a method. And we need to initialize the variable.
- We don’t require a reference to access the local variables.
- Local variables should be initialized and then only use.
- Only declaring not getting an error while using without initialization gets an error.
- If you are using local variables without initializing them then you will get an error.
Following are some examples of Local Variable -
Example 1:
class A{
public static void main(String [] args){
int i = 10; // Local Variable
System.out.println(i);
A a1 = new A();
a1.test();
}
public void test(){
System.out.println(i);
// Error because we created i as a local variable and accessed outside the method.
}
}
Output:-
10
Example 2:
class B{
public static void main(String [] args){
int i = 10; // Local Variable
System.out.println(i);
}
}
Output:-
10
Example 3:
class C{
public static void main(String [] args){
int i ;
System.out.println(i); // Error
}
}
Output:-
Local variables should be initialized and then only use.
Example 4:
class D{
public static void main(String [] args){
int i ; // Not getting any error
}
}
Output:-
Only declaring not getting an error while using without initialization gets an error.
Example 5:
class E{
public static void main(String [] args){
E.test();
System.out.println(i); // Error because i is a local variable used only on the test method
}
public static void test(){
int i = 10; // local variable
System.out.println(i);
}
}
Output:-
10
Reference Variable
A Reference variable is of two types
- Local Reference variable
- Static reference variable
- Reference variables are used to store memory addresses, they can never store ordinary value. Like 10.20 etc.
- Local Reference Variable:- These variables are created within a method and should be used only within the created method.
- Static Reference Variable:- These variables are created outside all the methods and inside the class using the static keyword. These variables can be used anywhere in the class as they have global access.
Following are the examples of Reference Variable:-
Example 1:
class A{
int i = 10;
public static void main(String [] args){
A a1 = new A(); // Local reference because created inside the method
System.out.println(a1.i);
a1.test():
}
public void test(){
System.out.println(a1.i); // Error
}
}
Output:-
Error because we are accessing local references in another method.
Example 2:
class B{
static B b1;
int i; // we will get the default value as zero
public static void main(String [] args){
b1 = new B();
System.out.println(b1.i);
b1.test();
}
public void test(){
System.out.println(b1.i);
}
}
Output:-
0
0
메타데이터
- post_id
- f2e0b4f87ad4
- slug
- variables-in-java-f2e0b4f87ad4
- url
- https://medium.com/@manisha10850/variables-in-java-f2e0b4f87ad4
- canonical_url
- https://medium.com/@manisha10850/variables-in-java-f2e0b4f87ad4
- author_url
- https://medium.com/@manisha10850
- status
- ok
- fetched_at
- 2026-07-26 12:34:43