• 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
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Variable scoping

 
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello all,
In the following code compiler is showing errors(Indicated in comments...)


Can any one explain these errors or suggest me some references ,
I think this behavior is different from other languages....

Thanks in advance...
 
Rancher
Posts: 618
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Here's your code formatted better. All the errors are "Duplicate local variable i". The int i declared on line 3 is a class instance variable. The semicolon on line 8 ends the for loop. Was this intentional? The int i declared on line 9 is a method local variable. The int i declared on line 12 is declared in a code block. Java does not allow variables to be re-declared in code blocks because this is more often than not a bug. That is, the developer does not intend to re-declare the variable. The same rule applies for the int i declared on line 20. The int i declared on line 23 that was commented out also causes a compiler error, despite the comment that says it does not. This one is trying to declare an int i as a class instance variable but one is already declared on line 3
 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Tom Reilly ,
Thank you for reply....
I am going to make code neater..

Semicolon on line 3 is intentional, as the for loop uses another variable i,
It just mean do-nothing statement...

variable declared at 23 , i mean to say if we declare the variable instead at (3) then there is no error...




 
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Continuing to what Tom has already mentioned - If you have access to any particular variable in any method or any block of data, you can not declare a variable with same properties. so form the code you have mentioned -



if you think over it, you will find that this is correct behavior...




 
Hareendra Reddy
Ranch Hand
Posts: 173
Firefox Browser Fedora Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Sunny Jain ,
Can i confirm that if we change the code in the following manner ,then there wont be error..

 
Deepak Chopra
Ranch Hand
Posts: 433
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi Hareendra ,

Yes If you write following piece of code, you will not get any error-


You can also consider the way an object is initialized in java -

1) Static block of super class executes

2) Static block of Subclass executes

3) Inside constructor of subclass

4) call to super constructor

5) call of Object Class constructor

6) Instance Variable of super class executes

7) init blocks of super class executes,in the order in which they appear

8) Super class constructor executes

9) Instance Variable of Subclass are initialized

10) Init blocks of subclass executes, in the order in which they appear

11) Subclass constructor executes

Source - Object Initialization in JAVA
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic