• 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Junilu Lacar
  • Liutauras Vilda
Sheriffs:
  • Paul Clapham
  • Jeanne Boyarsky
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
Bartenders:
  • Jesse Duncan
  • Frits Walraven
  • Mikalai Zaikin

can we initialise static variables in static initialization block ?

 
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 27235
87
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks guys, i understand
 
pie. tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic