Friday, September 18, 2009

Most Common Scoping errors commited by Novice Java Programmers

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!!!.

Suggested Links:-

Why Java does not support Multiple Inheritance
Why Java classes are not marked final
Variable Shadowing Complexities

Sponsored Link:-

Are you Intelligent Check!!

0 comments:

About This Blog

This Blog is all about Java and programming.This blog is written by Vaibhav Pandey(Read More about him) .He is a Computer Science Graduate .You can contact the author at javatute@gmail.com for any suggestion or Query.You can also contact the author for advertising on this blog.All the material presented here is the property of author and its reproduction in any form is strictly prohibited.

Disclaimer:-Download links provided here are not of author in any means.These are found over the Internet.
Page copy protected against web site content infringement by Copyscape

  © Blogger templates The Professional Template by Ourblogtemplates.com 2008

Back to TOP