• Post Reply Bookmark Topic Watch Topic
  • New Topic
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can't we use static block x anywhere?
Then what is the purpose of define such one?
 
Srinivasan thoyyeti
Ranch Hand
Posts: 558
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that's great!!! Thank you very much Srini & John.
 
Deepak Bala
Bartender
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6663
5
MyEclipse IDE Firefox Browser Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
reply
    Bookmark Topic Watch Topic
  • New Topic