Link to home
Start Free TrialLog in
Avatar of Embt
Embt

asked on

static block(Simple)

class Q8{

int i = 20;

static{
int i = 10;
}

public static void main(String args[]){

Q8 q = new Q8();
System.out.println(q.i);

}
}

How do I understand the variable i in the static block? how do I refer to it?
Avatar of adam923
adam923

You're using a static initializer block, it's like a constructor for the entire class -- called when the class is first used.  The int i in the block though makes a local variable that just dies after the block.  Maybe you want to do this?
static int i;
static{
     i = 20;
    }
but this is much easier and less confusing to write as
static int i = 20;
the block format is if you have to do something more complicated to static variables to initialize them.
Avatar of Embt

ASKER

then how to access the varible i in the static block in this case?

Thanks,
if you declare int i inside any block it's gone... if you declare it before the block you can just use it as a static field  Classname.i
>>>You're using a static initializer block, it's like a constructor for the entire class -- called when the class is first used.

This is the best description I have read of a static initializer,and it is used to initialize static variables and static metjhods like this.

private static final int number_Of_Names = 22;
private static final String NAMES[] = new String[number_Of_Names];

static
{      NAMES[ NUMBER ]                   = "Number";
      NAMES[ DESCRIPTION ]       = "Description";
      NAMES[ SERVICE ]             = "service";
......//code

fillThisFieldValues();
}

private static final void fillThisFieldValues()
{
//more code
}
>> then how to access the varible i in the static block in this case?

declare it static ? :)

static int i = 20;
You are actually using two variables:

the first one within the object scope (int i = 20) which is accesible by newly created objects of your class (e.g. Q8 q = new Q8(); - thats why your code returns 20)

the second one within the scope of the static initializer (int i =10) which can only be accessed from there (if you put System.out.println(i); into the static initializer it will return 10)

And, as pointed out in the previous comments, if you declare it 'static' at class level and assign it a value in your static initializer it would end up within the scope of the class and can be accessed by all new objects.
ASKER CERTIFIED SOLUTION
Avatar of ramshakal
ramshakal

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what new information did you give that makes your suggestions an "answer" while the previous were "comments"?
What additional thing do you need to make it answer. I think you got your answer already. So its better to close it. Why keep the topic open if you got your answer.
i'm not the person who asked the question, i'm one of the ones who responded with a comment
and my question still stands... what makes your response so much better than the comments above that it should be an answer?  leaving the question open lets the one who asked decide which he wants to accept
etiquette :)
adam923,
Yeah I also felt like you initially when such people barge in with a 'answer' but have to learn to forget such 'minor' things.

What say heyhey? aren't you a pastmaster at such minor irritations? full credit to heyhey for telling me abt such things.

:-)
Avatar of Embt

ASKER

Thanks for the discussion.

btw, I posted this question for Sun Certified Java Programmer exam. I have passed the exam this morning. thanks all for your help.

adam923:

I am sorry I am unable to give you the credit, although I think you deserve it.
embt- that's ok, in the future please realize that as the asker of the question you have complete control over who gets the points.  most people on this system will only post comments until the asker feels like promoting a comment to an answer.  if you ask the asker feel you don't want to award someone points all you have to do is reject the answer if you think a prior comment is equally worthy
>>>I am sorry I am unable to give you the credit, although I think you deserve it.

Give credit where credit is due.
:-)