wood burning stoves*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes static doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "static doubt " Watch "static doubt " New topic
Author

static doubt

raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135

When I run the code, the output is 3.
Which x is incremented in main & myMethod? x in static block or static x? But going from the answer to question, it seems static x is incremented !!!

Can anybody explain what is going inside code please!?!
Thanks


live
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
public class Sta {
static {int x =5;}
static int x,y;
public static void main(String[] args) {
x--;
myMethod();
System.out.println(x + y + ++x);
}
public static void myMethod() {
y = x++ + ++x;
}
}

1) static { int x = 5; } --> is valid only with in the;Out of the block you can't access.

2) static int x; is the one which you are modifying.
2.1) x--; // Now x is -1
2.2) y = x++ + ++x; // Now y= -1 + 1;So y=0. and x=1
2.3)System.out.println(x + y + ++x); // 1 + 0 + 2 --> 3


Thanks & Regards, T.Srinivasan
SCWCD 1.4(89%), SCJP 5.0(75%)
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

static {int x =5;}


This is local to the static block and is never used. So it is the other x that is used by your application


SCJP 6 articles - SCJP 5/6 mock exams - More SCJP Mocks
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
Can't we use static block x anywhere?
Then what is the purpose of define such one?
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi,

Given>
Class.forName("package.OracleDriver");
Connection con = DriverManager.getConnection("url",user,pass);

How DriverManager knows that OracleDriver is there?
In its static block OracleDriver, will get registered with DriverManager.

One of my friends told me. Correct me if i am wrong.
Lets Rock
raja kanak
Ranch Hand

Joined: Oct 18, 2006
Posts: 135
that's great!!! Thank you very much Srini & John.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

How DriverManager knows that OracleDriver is there?
In its static block OracleDriver, will get registered with DriverManager.


The driver is loaded and registered with Class.forName() and the example can be confusing to some one taking a SCJP. I dont think this has anything to do with static blocks.

Can't we use static block x anywhere?
Then what is the purpose of define such one?


Use the static block when you want to initialize something once, when any instance of this class is loaded or used. For example, you want to calculate a number only once and make it public static. After that your class instances can use this number. You can use it under such circumstances. The variable x in the static block is not useful here.
Srinivasan thoyyeti
Ranch Hand

Joined: Feb 15, 2007
Posts: 557
Hi John,
The driver is loaded and registered with Class.forName() and the example can be confusing to some one taking a SCJP. I dont think this has anything to do with static blocks.


I have a given a real world example. I really don't understand why you said
I dont think this has anything to do with static blocks.

Please explain me.
Deepak Bala
Bartender

Joined: Feb 24, 2006
Posts: 6661
    
    5

I dont think this has anything to do with static blocks.


My humble apologies. That sentence sounds quite wrong. In the context of the SCJP, concepts of JDBC could be a bit hard to understand if the person has never connected to a database using JDBC. You are right in the fact that the static block is used to register the driver. More specifically the DriverManager.registerDriver() method is used from the block after an instance of the driver is instantiated. Sorry
 
jQuery in Action, 2nd edition
 
subject: static doubt