| Author |
can we initialise static variables in static initialization block ?
|
jami siva
Ranch Hand
Joined: Oct 16, 2009
Posts: 56
|
|
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
Bartender
Joined: Oct 14, 2005
Posts: 16487
|
|
|
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
Joined: Nov 06, 2000
Posts: 177
|
|
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
Joined: Nov 06, 2000
Posts: 177
|
|
|
(You get simultaneous responses when you write your response, take a phone call, then press Submit when you hang up.)
|
 |
jami siva
Ranch Hand
Joined: Oct 16, 2009
Posts: 56
|
|
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
Joined: Nov 06, 2000
Posts: 177
|
|
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
Joined: Sep 19, 2008
Posts: 377
|
|
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.
|
SCJP 6,SCWCD 5,SCBCD 5
Failure is not an option.
|
 |
Rahul P Kumar
Ranch Hand
Joined: Sep 26, 2009
Posts: 188
|
|
|
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
Joined: Oct 16, 2009
Posts: 56
|
|
|
Thanks guys, i understand
|
 |
 |
|
|
subject: can we initialise static variables in static initialization block ?
|
|
|