Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

static block reading values from other class static block

 
sanju dharma
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
I have one static class , say , Database.java and other class DbConnection.java. I have static block in both classes. Can I use static variable values from one class into static block of another class. I mean what happens if second class gets loaded ( JVM ) earlier than second class. Will first class's value be null ? For example
public class DbConnection {
static {
if(Database.bInsert)
// do something
}
what will happen if DbConnection gets loaded first than Database. Will bInsert value null/default?
Thanks,
Sudhir
 
Stan James
(instanceof Sidekick)
Ranch Hand
Posts: 8791
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Give it a try. Let's say A references B in a static block. The first time you reference A its static block is evaluated, and it references B so B's static block is evaluated before you can "get" anything from it. Ought to work fine. If for some reason you reference B first, it will be initialized before A, and that's just fine.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic