newbie troubles with static

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


Question by:vlg

Accepted Solution

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)

   boolean redsTurn = true;

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

Expert Comment

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

Author Comment

ID: 6949420
Hello jlouwere

Thanks - I'll re-examine.


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