Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

newbie troubles with static

Posted on 2002-04-17
3
Medium Priority
?
174 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 5

Accepted Solution

by:
Jan Louwerens earned 400 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

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

610 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