Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

can we initialise static variables in static initialization block ?

 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi JavaAchievers,

im trying to compile the following code,

class Chap3Ex7
{
static
{
static int i=15;
}

{
int i=12;
}

public static void main(String[] args)
{
Chap3Ex7 ch = new Chap3Ex7();
}
}

im getting something related to :

Chap3Ex7.java:6: illegal start of expression
static int i=15;
^
my question is can't we use static variables in static block ,whenever i remove static keyword it is working fine,
i declared same variable i in both Initialisation blocks , i think that is not the problem because of both are having different blocks

Any help please....


Thanks
Sivva
 
Paul Clapham
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sure you can initialize a static variable in a static initializer. You just can't declare a static variable in a static initializer. The latter is what you tried to do. Replace your code by some code which declares a static variable, then uses a static initializer to initialize it.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You've defined the member inside the static block. You need to define it outside the block, and within the block, simply reference it.
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(You get simultaneous responses when you write your response, take a phone call, then press Submit when you hang up.)
 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes, its working Paul,

i changed my code like this its working

static int i;
static {
i=12;
}

is any more explanation for why we can'nt declare static variables in static block
 
Max Rahder
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
jami siva wrote:is any more explanation for why we can'nt declare static variables in static block

You are allowed to code a local variable in a block, but you can't declare a member in any block. As far as "why" -- that's just the rule. The Java Language Specification says "A block is a sequence of statements, local class declarations and local variable declaration statements within braces." So, members, such as static fields, can't be declared in a block.
 
Siva Masilamani
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
All the variable declared inside block,method are called Local and the only modifier allowed for local variable is final.

No public,private.... or transient,static,volatile etc are allowed for local variable.

 
Rahul P Kumar
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This rule exists because any variable declared within curly braces has scope till those braces. Now when you declare a variable static, you are making it a class variable, which has wider scope than local or instance variable. If static variable was allowed, it could have become conflict of interest. So the rule.
 
jami siva
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks guys, i understand
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic