wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes scope of variables Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "scope of variables" Watch "scope of variables" New topic
Author

scope of variables

madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
hi!
in the code the decl. of i in the static block does not give error.
but in the for loop decl of j gives error.
in the Mughal book it's given that the same variable should not be in the same or enclosing block.therefore its understandable for the for loop but why is it right in the static block
since i=20 is present in the enclosing block
class ScopeDemo1
{
static int i = 20;
static
{
int i = 10;

}
public static void main(String[] args)
{
ScopeDemo1 a = new ScopeDemo1();
System.out.println(a.i);
int j=23;
for(int j=98;j<100;j++)
{
}
}
}
madhur
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
This could be a case of "hiding static variables" just the same as if you redefined the static variable i in the body of a method...
-Barry
Changed x -> i
[ August 29, 2002: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
class ScopeDemo1
{
static int i = 20;
static
{
int i = 10;
The first i above is within the {} of class. The
second is within the {} of static block. So different levels. The second i will hide the first one.
int j=23;
for(int j=98;j<100;j++)
However, both j above are in same {}. Therefore, it not legal.
Hope this helps.
Barkat
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
Hi Guys,
Looks to me like even the i variable should give an error. In fact, the i variable is already declared in an enclosing block that is the class block and therefore visible in the nested block which is the static block. Declaring the i variable after the static block should solve the problem.
For the same reason declaring the j variable in the for block is illegal.


Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
madhur jain
Ranch Hand

Joined: Aug 27, 2002
Posts: 49
well i odes not give compile error.
and j does .
that's the truth
but i don't knowhy.
madhur.
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi there:
Looks to me like even the i variable should give an error. In fact, the i variable is already declared in an enclosing block that is the class block and therefore visible in the nested block which is the static block.

You can redefine a variable in nested block. But you can not redefine it in the same block. That is
why j is giving error.
Thanks
Barkat
[ August 30, 2002: Message edited by: Barkat Mardhani ]
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
If you change the for loop to:
for(j=98;j<100;j++)
It should compile fine. Now you are re-assigning j, not redeclaring...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: scope of variables
 
Similar Threads
strange casting and conversions
Static Declaration
Initialization in with in for block
the "break" statement
Static Int and Methods