Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Doubt in static...

 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Result is 20
But how, I am little confused here as i thought result should be 10....please make me correct... (Source-Geocities.com)
 
sudipto shekhar
Ranch Hand
Posts: 823
Chrome Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Hemnathbabu Kottaiveedu
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
sunil langeh
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Friends
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
David Marco
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Men Gumani
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My mistake
 
Pete Delrue
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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;
 
Bob Wheeler
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks David.
 
Sumit Bisht
Ranch Hand
Posts: 331
Python Ruby Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic