This week's book giveaway is in the Other Languages forum.
We're giving away four copies of Rust Web Development and have Bastian Gruber on-line!
See this thread for details.
Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic