Click to Play

Creating a Self-Service Sales Cycle
What happens if your onsite analytics look good, but you still aren't drawing conversions? Bryan Eisenberg of FutureNow encountered that very...

Recent Articles

Preparing For Your SCJP Exam
The SCJP 310-065 exam is not easy to crack. I have an experience of using the UCertify’s SCJP 310-065 exam preparation kit for quite a long time,and it is needed the best exam preparation kit available. I have...

The Truth Of Being Sun Certified
Being a Sun Certified Java Programmer(SCJP) is undoubtedly a matter a pride, But due to the immense increase in qualified Human Resource especially in IT/ITES sector, it is no longer a matter of pride. Once upon a time (2-3 years back) we knew that...

Preparing For Your Windows Server...
Note: These instructions are for Microsoft Windows Servers. Linux or Apache 301 Redirect Instructions can be found here. Redirecting pages on a Windows Server can be complex, but it is still highly recommended...

Addressing The Real World Educational Needs...
What has been interesting is the amount of interest in the information security program that we have written lately, and this includes how we are using technology in an educational environment, as well as the view of information security and who we are as an industry.

Implementing An Open Certification Standard...
Rajiv Shah and Jay Kesan wrote the paper "running code as part of an open standards policy" arguing that the "running code" requirement – i.e. multiple independent, interoperable implementations of an open standard...

Should Maintaining The Accuracy Of IT...
You are only as safe as your expert opinion . But then the question is, what if the expert opinion is followed, and you are certified and you still get a data breach that costs the company millions of dollars. Wired threat...

The Understanding And Implementation Of...
I have been to my share of conferences and webinars on cloud computing the last two years and I have also read the content of many colleagues who live blog, blog, and/or tweet at cloud conferences as well. What I am finding is that most of the talking is being...



09.18.09

Java Programmer – Common Scoping Errors

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.

eMetrics Marketing Optimization Summit
Register Now

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

Comments


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.
About ITCertificationNews
A collection of resources designed to assist IT professionals evaluating various certification programs within the IT world. IT Certification Articles and UPdates




ITCertificationNews is brought to you by:
SecurityConfig.com NetworkingFiles.com
NetworkNewz.com WebProASP.com
DatabaseProNews.com SQLProNews.com
ITcertificationNews.com SysAdminNews.com
DevNewz.com WirelessProNews.com
CProgrammingTrends.com ITmanagementNews.com



-- ITCertificationNews is an iEntry, Inc. publication --
iEntry, Inc. 2549 Richmond Rd. Lexington KY, 40509
2009 iEntry, Inc.  All Rights Reserved  Privacy Policy  Legal

archives | advertising info | news headlines | free newsletters | comments/feedback | submit article



IT Certification Articles and UPdates ITCertificationNews ITCertificationNews Home Page About Article Archive News Downloads WebProWorld Forums Jayde iEntry Advertise Contact