1

If I were to do something such as:

public class Game
{
    private boolean RUNNING = true;

    Game()
    {
    }

    public static void main(String[] args)
    {
        Game game = new Game();
    }
}

At what point in time would RUNNING = true?

edit: for clarity, at what point in the program would running be set to true. ex: Before the constructor, after the constructor, etc.

5
  • 8
    All the time?... Commented May 26, 2013 at 18:05
  • It's a good question. When you create an object there's a certain order how things are initialized. It's not just "all the time", I'm sorry. Commented May 26, 2013 at 18:08
  • That's what I was asking about. The order, I mean. Commented May 26, 2013 at 18:09
  • @Lemmons In that case, I'd update the question to reflect ordering and such, otherwise, it's not clear from the title. Commented May 26, 2013 at 18:10
  • 2
    @ThomasUhrig: With a more comprehensive example code (e.g. with multiple members to initialize, static initializers, etc.), it might be a good question ;) Commented May 26, 2013 at 18:17

5 Answers 5

4

It will be set to true before the constructor. You can use it in the constructor as true.

Sign up to request clarification or add additional context in comments.

Comments

1

This code explains itself:

public class SuperClass
{
    String superField = getString("superField");

    public SuperClass()
    {
        System.out.println("SuperClass()");
    }

    public static String getString(String fieldName)
    {
        System.out.println(fieldName + " is set");
        return "";
    }

    public static void main(String[] args)
    {
        new ChildClass();
    }
}

class ChildClass extends SuperClass
{
    String childField = getString("childField");

    public ChildClass()
    {
        System.out.println("ChildClass()");
    }
}

OUTPUT:

superField is set
SuperClass()
childField is set
ChildClass()

Comments

0

When the constructor is called using the new operator all non-static members of the class are initialized before the code inside the constructor is executed. You can use the debugger and step into that call and see where it goes first. Static members are initialized when the class is loaded and for the first time accessed (see this question for more detailed info about static members).

Comments

0
private boolean RUNNING = true;
Game() {
}

is exactly the same as

private boolean RUNNING;
Game() {
    RUNNING = true;
}

Actually, the comiler will move the initialization at the beginning of the constructor. The value will then be set when instantiating an object of that class.

Comments

0

When you try to use local variables which not manually initialized, you will get a compile time error.

public static void main(String args[]){
               int a;
              System.out.pritnln(a); //error
      }

But it's not the case with instance variables. This itself shows that they are ready for usage before the constructor even.

   public class Example{
            private int a;
            public Example(){
                   System.out.println(a);   //No error
            }
            public int getA(){
                   return a;           //No error
            }
   }

I hope this intuition answers your question..........

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.