*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Doubt in static... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Doubt in static..." Watch "Doubt in static..." New topic
Author

Doubt in static...

sunil langeh
Ranch Hand

Joined: Sep 04, 2007
Posts: 88


Result is 20
But how, I am little confused here as i thought result should be 10....please make me correct... (Source-Geocities.com)


Thanks
Sunil (SCJP 5)
sudipto shekhar
Ranch Hand

Joined: Apr 02, 2008
Posts: 818

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


Regards, Sud.
SCJP 5 ScjpFAQ JLS
Hemnathbabu Kottaiveedu
Greenhorn

Joined: Mar 13, 2008
Posts: 28
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


SCJP 5 (100%)
sunil langeh
Ranch Hand

Joined: Sep 04, 2007
Posts: 88
Thanks Friends
Bob Wheeler
Ranch Hand

Joined: Apr 24, 2009
Posts: 317
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


SCJP 6 - SCJD - SCWCD 5 - SCBCD 5
JavaEnterpriseEditionFaq - TomcatFaq
David Marco
Ranch Hand

Joined: Feb 23, 2009
Posts: 44
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.


SCJP 6
Men Gumani
Ranch Hand

Joined: Apr 01, 2009
Posts: 31

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

Joined: Feb 23, 2009
Posts: 44
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

Joined: Apr 01, 2009
Posts: 31
My mistake
Pete Delrue
Greenhorn

Joined: Apr 28, 2009
Posts: 2
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

Joined: Apr 24, 2009
Posts: 317
Thanks David.
Sumit Bisht
Ranch Hand

Joined: Jul 02, 2008
Posts: 329

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

Joined: Jan 28, 2008
Posts: 5575

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
 
 
subject: Doubt in static...
 
Similar Threads
from mock exam qs.
static block
static keyword over a block
Question on toString and Static
static block