Java Programmer – Common Scoping Errors
Posted by Vaibhav Pandey
Scoping errors comes in various sizes and shapes. Novice Java programmers most often commit some silly mistakes which are undesirable.The most common mistake novice programmers commit is scoping errors which comes in many ways. One is happens when the variable is shadowed and two scopes overlap.It becomes very much difficult to identify the problem.The reason why scoping errors comes into effect is when programmer attempts to access a variable not in scope or visibility.There are few common mistakes which are explained below.Lets take a look:-
1.Attempting to access an instance variable from a static context (i.e. from main() method)
Since static variable can only be accessed from static context thus there is no way you can access the non-static instance variable in static context without a reference.For example:-
Class Scopetest{
int x=10;
public static void main(String a[]){
System.out.println(x++);
}
}
This will result in an error
Scopetest.java:4: non-static variable x cannot be referenced from a static context
System.out.println(x++);
Despite this you can use the instance of Scopetest class to access the variable as shown below:-
class Scopetest{
int x=10;
public static void main(String a[]){
System.out.println(new Scopetest().x++);
}
}
This will produce output as 10.
So,remember you can never ever access a non-static instance variable within a static context alone.You have to use the enclosing class instance.
2.Attempting to use a block variable after the code block is completed.
How often we commit this error,I guess everyone of us has faced it.So remember never ever try to invoke a variable which is ended its life.That is block variable lifetime is until the code block completes.As block finished execution the variable will also perish from memory,thus any reference out of this block result in a compiler slap!!!
Lets take a look at following example:-
class Scopetest{
public static void main(String a[]){
for(i=0;i<2;i++) style=”font-style: italic;”>.java:5: cannot find symbol
symbol : variable i
location: class Scopetest
if(i<3)>
3.Attempting to access a local variable from nested method.
When a method invokes another method then the calling method does not have access to the called methods variables and vice versa.For example:-If we have a method do1() which called by another method do2() then do1() cannot access the variables local to do2().It is evident from the following example:-
class Scopetest{
public static void main(String[] a){
Scopetest s = new Scopetest();
s.do2();
}
public void do2(){
int x = 4;
do1();
++x;
}
public void do1(){
x++;
}
}
The error which appears is:-
Scopetest.java:12: cannot find symbol
symbol : variable x
location: class Scopetest
x++;
So next time you go to the battlefield(programming) keep yourself armed with better knowledge and strategies of course here is to reduce the bug and make your program more readable.Keep in mind above mentioned errors and be a good programmer,and keep firing on the COMPILER!!!.
About the Author: Vaibhav Pandey got offered employment from an Indian Multinational IT Company. He is 21 years old. He has a huge interest in Java programming and has liked it from his study days. Vaibhav loves to blog and share his experiences and thoughts. He now resides in Lucknow, a state capital in India. Check out his blog at http://javatutorialsworld.blogspot.com.
iEntry 10th Anniversary
Cloud Computing
Certification
Leave a Reply