• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

What's special about static initializer

 
Ranch Hand
Posts: 63
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class q {
int i = 10;
static int i =20; //(1) Duplicate variable declaration int i
}
class q1 {
int i =10;
static {
int i = 20; (2) No compile time error
}
}
If we compile the above code Line (1) gives a compile time error 'Duplicate variable declaration int i'
But if we put the static variable in a static intializer it works
Is it not funny ?
Can any one explain why it is so ?
Cheers
Monzy
 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi,
the code
<pre>
public class Test
{
public static void main(String[] args)
{
A a=new A();
}

}
class A
{
static int i=10;
static
{
int i=20;
System.out.println("local to class : " + i);
}

A()
{
int i=30;
System.out.println("local to constructor : " + i);
printI();
}

public static void printI()
{
System.out.println("local to printI() : " + i);
}
}
</pre>
when executed, will return a result of -

local to class : 20
local to constructor : 30
local to printI() : 10

conclusion -
int i and int i in the static initializer has no compilation errors because the first i is local to the class, while the second i is local to the static initializer. however, int i and static int i that is declared local to the class will result in "duplicate variable" error.
hope this helps
(please don't include me in the drawing for the book as my test is scheduled to be next tuesday *shivers*)
 
Ranch Hand
Posts: 5399
1
Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by MONZY THARIAN:
class q {
int i = 10;
static int i =20; //(1) Duplicate variable declaration int i
}
If we compile the above code Line (1) gives a compile time error 'Duplicate variable declaration int i'


This will give error as you can not have same identifier in the same scope.


class q1 {
int i =10;
static {
int i = 20; (2) No compile time error
}
}
But if we put the static variable in a static intializer it works


But here the moment you start a block of code (ie codes between two curly braces) you define different scope. So here in static block 'i' is hiding member varible 'i' and that's why no compilation error.
HTH

------------------
Regards
Ravish
 
reply
    Bookmark Topic Watch Topic
  • New Topic