?
Solved

newbie troubles with static

Posted on 2002-04-17
3
Medium Priority
?
173 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses
Course of the Month13 days, 18 hours left to enroll

801 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