• 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

static initialization block

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i wanted to know if the below code is legal:
class test2{
static {
static int i=0;
}
public static void main(String[] args)
{
}
}
i am getting a compiler error.
thanks in advance
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Namrata Shetty:
i wanted to know if the below code is legal:
class test2{
static {
static int i=0;
}
public static void main(String[] args)
{
}
}
i am getting a compiler error.
thanks in advance


I don't think it is possible to have static variables within any block of code e.g. not possible to have method local static variables.
 
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

It is possible to have static var. inside the static block but construction is not possible.
The following e.g will compile
class testy{
static int i;
static{
i=1; //i is static
}
public static void main(String[] args)
{
}
}

Originally posted by John McErkland:
I don't think it is possible to have static variables within any block of code e.g. not possible to have method local static variables.


 
Ranch Hand
Posts: 464
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static belongs to class.
So they cant be declared in a block or a static method
But they can be assigned.
HTH
 
reply
    Bookmark Topic Watch Topic
  • New Topic