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?
EmbtAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

adam923Commented:
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.
0
EmbtAuthor Commented:
then how to access the varible i in the static block in this case?

Thanks,
0
adam923Commented:
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
0
CompTIA Network+

Prepare for the CompTIA Network+ exam by learning how to troubleshoot, configure, and manage both wired and wireless networks.

mbormannCommented:
>>>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
}
0
heyhey_Commented:
>> then how to access the varible i in the static block in this case?

declare it static ? :)

static int i = 20;
0
HiThereCommented:
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.
0
ramshakalCommented:
>class Q8{
>
>         int i = 20;

>           static{
>                   int i = 10;
>           }

Hey since you are declaring i ( you have written int i) in static block
so its scope is limited to that block and you cannot access it out side that block. So it remains unaccessible in object of that class .

As adam923 has suggested you should do it.
static int i;
static{
     i = 20;
 }

But again since you are using i before this so writing "static int i" will give compile error. You cannot have two member of class with same name that is a simple programming rule. But your purpose wil be solved if you chage the name of variable as j and proceed as

class Q8{

   int i = 20;

  static int j;  
  static{
         j = 10;
  }

  public static void main(String args[]){

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

Now you have access to variable i and j, you have access to initialise value of j inside static block. tell me if you want something else.

 
 
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
adam923Commented:
what new information did you give that makes your suggestions an "answer" while the previous were "comments"?
0
ramshakalCommented:
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.
0
adam923Commented:
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
0
heyhey_Commented:
etiquette :)
0
mbormannCommented:
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.

:-)
0
EmbtAuthor Commented:
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.
0
adam923Commented:
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
0
mbormannCommented:
>>>I am sorry I am unable to give you the credit, although I think you deserve it.

Give credit where credit is due.
:-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.