• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Junilu Lacar
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst
  • Peter Rooke
  • Mikalai Zaikin
Bartenders:
  • Himai Minh

Shadowing variable doubt?

 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys,

Consider the code below,



Why it gives compiler error at line 2?? Anyone please!
 
Ranch Hand
Posts: 72
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
In line 2, k is redeclared.
I dont think we can shadow a local variable by another local variable.
So, it should give a compilation error. Shadowing happens only when a local variable is declared with same name as an instance variable.

There should be a compilation error in line 3 also because "int" before j is not allowed.
 
Ranch Hand
Posts: 502
jQuery Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You have two variable with same name inside a single method which is not shawoding. you are duplicating the variable name. In your code make k as instance variable. It should work.

hth,
 
Joe San
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys for the help.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because you are declaring variable k twice in this method
once on first line of main method
int k;

and another on line# 2
for (int i=0, k=0; i<2; i++,k++) {System.out.print(i);}

here, int i=0, k=0 ==> int =0 and int k=0;

Regards!
 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I compiled the program given,but i am getting error at line 3


class Test {

public static void main(String args[]) {

int k;

for (int i=0, j=0; i<2; i++,j++) {System.out.print(i);} // 1
for (int i=0, k=0; i<2; i++,k++) {System.out.print(i);} // 2
for (int i=0, int j=0; i<2; i++,j++) {System.out.print(i);} // 3 cant have multiple declaration

}}
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, well, the error pretty much tells you what you need to know: you can't do that. And you've already found alternate ways to do the same thing you were trying to do. They don't want you to repeat the "int" part of the declaration because if you did that, you could also write a loop that declares an int and a String, for example. Not that this is inherently wrong, necessarily, but the language designers made a conscious choice to limit how much complexity you are allowed to put into a single for statement. They allow more than one variable to be initialized, only if the variables are of the same type - and in that case, they can share the type declaration (the word "int" in this case); you don't have to repeat it.
reply
    Bookmark Topic Watch Topic
  • New Topic