Forums Register Login

Doubt in static...

+Pie Number of slices to send: Send


Result is 20
But how, I am little confused here as i thought result should be 10....please make me correct... (Source-Geocities.com)
+Pie Number of slices to send: Send
First you are assigning value to variable i in a static block,which is invoked when the class is first loaded. Then when you create an instance of the class, the variable i which is for that instance is assigned the value 20,at the time of instance creation the static block is not invoked(like I said it's invoked only once when the class is loaded). Hence the answer 20....

Hope this helps
+Pie Number of slices to send: Send
HI Sunil,

The output of 20 is correct. You need to understand in this program that there are two variables 'i' with the same name. one is an instance variable(int i=20) and the other is a local variable (scope limited to the static block). Now in the main method an instance of class is created and using that instance the variable i is accessed and hence the value is 20.

The variable i declared in the static block cannot be accessed from the main method because of its limited scope. You can modify the above program as:


Now the output will be 10 because the scope of variable j is now extended and it can be accessed anywhere within the same package.

Thanks,
Hemnath
+Pie Number of slices to send: Send
Thanks Friends
+Pie Number of slices to send: Send
What about this code?

Now we have two assignments for the same static variable i. Why is the assignment in the static block comes second? Does the declaration of the class triggers first the i=10 assigment and then the static block?

Thanks and cheers
Bob
+Pie Number of slices to send: Send
Hi Bob, the static block is reached last because all field initializers and initialization blocks are executed in the order in which they occur in the class declaration. Also, a variable can be declared after they are initialized (if the last occurs in a block), . So the below two snippets compile and run, although they produces different outputs:




The same is true for instance (non-static) variables. However the follow don't compile:



And the same is true for instance variables, too.

Greetings.
+Pie Number of slices to send: Send

confuses me.


this should produce the output of 5 instead of 10 and the order should not matter should it?

Aside, I vaguely remember that some variable will be assigned a value after the constructor is called,

is the following flow correct when you create a new trial object?
1) give i a default value 0
2) run the constuctor
3) assign 5 to i since constructor does not initialize i
sounds weird to myself
+Pie Number of slices to send: Send
Hi Men Gumani, in your code you're creating two different variables: a class variable and a local variable:



Finally, your procedure flow sounds well.


+Pie Number of slices to send: Send
My mistake
+Pie Number of slices to send: Send
in the example where the output is five, why isnt there an error as there would be with non-static variables?

static {
j=10;}
static int j=5;

vs

j=10; --> compiler error
int j=5;
+Pie Number of slices to send: Send
Thanks David.
+Pie Number of slices to send: Send
Pete, the code with static initializer is correct

static{
j=10;
}
static int j=5;

However, a similar thing happens with non static members.So while your code
j=10; --> compiler error
int j=5;
is giving a compile error, this would not :
{
j=10;
}
int j=5; //after an object is created, initialize j, set its value to 10, then again set j to 5 (before running its constructor)
+Pie Number of slices to send: Send
 

sunil langeh wrote:
Result is 20
But how, I am little confused here as i thought result should be 10....please make me correct...



Simple . It is all about lifetime of the variable
Life just hasn't been the same since the volcano erupted and now the air is full of tiny ads.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1148 times.
Similar Threads
from mock exam qs.
static block
static block
Question on toString and Static
static keyword over a block
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 02:43:19.