Solved

newbie troubles with static

Posted on 2002-04-17
3
168 Views
Last Modified: 2010-03-31
Trying to do things the right way, but I'm getting killed.
First I had:
class Board
{
  static boolean redsTurn;
  //more stuff, unrelated
     
  public Board()
    {
      Board.redsTurn = true;
      //more unrelated stuff
    }
   //...
}

and it couldn't resolve redsTurn.  How is that possible?  I tried it with and without Board. in front.

Then I tried:
class Board
{
    static
    {
      boolean redsTurn = true;
    }//close static

    public Board()
    {
         Board.redsTurn = true;
        //...
    }
}
and it still couldn't resolve redsTurn, with or without Board. in front.  I also tried this. in front just for kicks.
What the big secret with static?

Thanks

v
0
Comment
Question by:vlg
3 Comments
 
LVL 5

Accepted Solution

by:
Jan Louwerens earned 100 total points
ID: 6949305
It makes sense that the second example doesn't work, since you're declaring a local variable within the scope of the static block. (That static block doesn't declare variables, it only runs some code the very first time this class is used)

static
{ // START SCOPE
   boolean redsTurn = true;
} // END SCOPE


However, the first case worked for me with both:
   Board.redsTurn = true;
and
   redsTurn = true;
inside the constructor.
Perhaps it's a problem with your particular compiler?
0
 
LVL 92

Expert Comment

by:objects
ID: 6949316
Agreed, the first case is fine and should work.
0
 

Author Comment

by:vlg
ID: 6949420
Hello jlouwere

Thanks - I'll re-examine.

v
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This video teaches viewers about errors in exception handling.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

27 Experts available now in Live!

Get 1:1 Help Now