Solved

newbie troubles with static

Posted on 2002-04-17
3
171 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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

726 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