Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# static doubt

Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:

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

Ranch Hand
Posts: 558
• Number of slices to send:
Optional 'thank-you' note:
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

Bartender
Posts: 6663
5
• Number of slices to send:
Optional 'thank-you' note:

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

raja kanak
Ranch Hand
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
Can't we use static block x anywhere?
Then what is the purpose of define such one?

Srinivasan thoyyeti
Ranch Hand
Posts: 558
• Number of slices to send:
Optional 'thank-you' note:
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
Posts: 135
• Number of slices to send:
Optional 'thank-you' note:
that's great!!! Thank you very much Srini & John.

Deepak Bala
Bartender
Posts: 6663
5
• Number of slices to send:
Optional 'thank-you' note:

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
Posts: 558
• Number of slices to send:
Optional 'thank-you' note:
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.

Deepak Bala
Bartender
Posts: 6663
5
• Number of slices to send:
Optional 'thank-you' note:

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

 Don't get me started about those stupid light bulbs.