Solved

newbie troubles with static

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
word0 challenge 3 58
for i loop in grovy 1 32
ArrayIndexOutOfBoundException 9 35
Java Jpanels and Jframe 8 20
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…

760 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

22 Experts available now in Live!

Get 1:1 Help Now