-1

I apologize if this is redundant, but I was not able to find a similar question. And, TBH, I don't even know how to frame the question properly.

This is a from a review question from the Java 8 OCA study guide. The question is about static initializers, which I understand just fine. There is however a line of code that I don't get, and because the question isn't about it, there is not a very good explanation of it.

private static Rope rope = new Rope();

So this isn't about Singletons or static classes. I don't understand why you would initialize an object like this. And, if there is a good reason why you can and would do this, what is the reason?

Would someone kindly point me in the direction of an explanation? Like I said, I'm not even sure what this is properly called, so am having a hard time finding a good answer on my own.


Edit to put in the entire class:

import rope.*;
import static rope.Rope.*;

public class RopeSwing
{
  private static Rope rope1 = new Rope("rope 1");
  private static Rope rope2 = new Rope("rope 2");

  {
    System.out.println(rope1.length);
  }

  public static void main(String[] args) {
    rope1.length = 2;
    rope2.length = 8;
    System.out.println(rope1.length);
  }
}
dimo414
  • 47,227
  • 18
  • 148
  • 244
  • 2
    `rope` is not a 'non-static object'. The fact that `Rope` isn't a static class is irrelevant. – user207421 Jun 24 '17 at 17:57
  • I removed the line about `Rope` not being a "static class", since people seem to be getting hung up on that fact. It's an understandable confusion, but not related to the syntax you're seeing. – dimo414 Jun 24 '17 at 18:09

1 Answers1

0

This makes a single Rope instance available to the whole class - it will be shared by all instances of the class this is declared in. This can be useful when there's some shared information or state all instances should rely on.

Often, private static fields are also declare final, which makes them constants (assuming the type of the field is immutable). Looking at your full example, I suspect the author should have made them private static final.

For example:

public class Foo {
  private static Rope rope = new Rope();
  private int value;

  public Foo(int value) { this.value = value; }

  @Override public String toString() {
    return "static rope: " + rope + " instance value: " + value;
  }
}

If you create several instances of Foo (new Foo(1);, 'new Foo(2), etc.) they will all share the same rope instance, and new Rope() will only have been invoked once (when the class is first loaded).


An example of a non-constant static field might be a shared counter. Suppse you want to uniquely identify every instance of an object that gets constructed, anywhere in your application. You can do so with an AtomicInteger, which is essentially a thread-safe int:

public class Unique {
  // despite being final this is not a "constant" because it's mutable
  private static final AtomicInteger counter = new AtomicInteger();
  private final int id;

  public Unique() {
    id = counter.getAndIncrement();
  }

  @Override public String toString() { return "ID: " + id; }
}

Give it a try - every instance of Unique will have a unique ID.


In your example code there's an instance initializer, which will be invoked when creating a new instance (hence after the static fields have been initialized).

dimo414
  • 47,227
  • 18
  • 148
  • 244
  • Let me say that back at you to make sure I get it: Because the initializer is declared static, it can be accessed without an instance of the class it's written in being constructed. – eightarcher Jun 24 '17 at 17:53
  • Well, sort of. Because the *field* is declared static it will be initialized when the class is first loaded, before any instances of the class are constructed. But because it's `private` nothing can *access* the field except instances of the class defining the `private static` field. – dimo414 Jun 24 '17 at 18:10
  • That's pretty cool. Thank you. I believe I'm getting it now. Thanks for the help. The test review questions are full of "Gotcha" code like that that just leaves me scratching my head. – eightarcher Jun 24 '17 at 19:30