As i discussed in last post there can be two types variables depending on value they store, such as reference and primitves. In this post i am going to discuss different types of variables depending on their scope.
Following are the kinds of variables categorized according to their scope
- Static variables
- Instance variables
- Method local and Method parameters
- Block variables
Following code shows the variables on go [Code Snippet 1]
public class Variables { static int staticVariable = 1; int instanceVariable = 2; public void methodName(int methodParameter) { int methodLocalVariable = 3; if (true) { int blockVariable = 4; } } }
Static variables
These variables are declared at the top level. They begins their life when first class loaded into memory and ends when class is unloaded. As they remain in memory till class exist, so these variables often called Class variables. There only one copy of these variables exist
These variables are having highest scope that is they can be accessed from any method/ block in a class
When no explicit assignment made while declaration they are initialized to default values , depending on their type.
E.g. As from Code Snippet 1, staticVariable declared as the variable of this type.
Instance variables
These are also declared as the top level variable. They begins their life when object/.instance of class created and ends when object is destroyed. Each copy of this variables belongs an object.
Same as static ones, they initialized to default values depending on their type
They are having less scope as compared, they are accessed only within instance method blocks.
E.g. As from Code Snippet 1, instanceVariable is of this type.
Method local variables / Method parameters
Method local variables are declared anywhere inside method. their life is started point they declared / initialized and ends when method completes.
These variables are not assigned any default value as case in case of static and instance variables. therefore, they must be initialized when declared or later but before accessing/ using in an expression otherwise compiler will complain.
// other code of class public void methodName(int methodParameter) { int methodLocalVariable ; System.out.println(methodLocalVariable); } // other code of class // If you try to compile above code snippet, compiler will throw following // VariablesDemo.java:9: variable methodLocalVariable might not have been initialized // System.out.println(methodLocalVariable); // ^
Method parameters are local variables to method only except their declaration in parameter list of method and they gets value upon invocation.
They are only accessible only in method that they are declared.
e..g As from Code Snippet 1, methodLocalVariable and methodParameter is of this type
Block variables
These are variables that are declared inside any block. They can be accessed only within that block only.
Just like method local variables, they are not assigned any default value. Therefore, It is mandatory to assign value prior to using same in expression/ accessing.
e.g. As from Code Snippet 1, blockVariable is this kind.
Nice tutorial abhi. Its important to choose what kind of variable you should and when, Also when to avoid static variables. I have also have a post on differences between class, member and local variables in Java. let me know how do you find it.
Thanks for comment Javin.
Yes, its important to choose which kind of variable as static one has highest scope and
thats nice post from you .
Hey nice post abhi not only for beginners but also for experienced people for basics.
Nice writeup. There are at least two more interesting variable scopes also: final method variable and non-static inner class.
A final method variable can be used from within the method body of anonymous classes, unlike non-final method variables.
public Runnable myMethod() {
// error if this isn’t final
final String someValue = “ohai”;
Runnable runnable = new Runnable() {
public void run() {
System.out.println(someValue);
}
}
return runnable;
}
And a non-static inner class can access instance variables on its outer (parent) class–this is the same as an anonymous class.
public class MyClass {
String outerValue = “hi there”;
private class MyNestedClass {
public void doSomething() {
System.out.println(outerValue);
}
}
public void doIt() {
new MyNestedClass().doSomething();
}
}
Cheers.
Interesting example of using different types in java. Great post. Thank you for sharing this info.
thanks !
thanks for the good article.. 🙂