0

When studying the jdk source code (jdk 1.8.0_111), i found a piece of strange code as follow:

public class AtmicBoolean implements Serializable {

    private static final long serialVersionUID = 4654671469794556979L;
    private static final Unsafe unsafe = ;
    private static final long valueOffset;
    private volatile int value;

    static {
        try {
            valueOffset = unsafe.objectFieldOffset(AtomicBoolean.class.getDeclaredField("value"));
        } catch (Exception localException) {
            throw new Error(localException);
        }
    }
  // .... other codes
}

Question: where does the unsafe variable was set ?

Pieter
  • 895
  • 11
  • 22
Sampson
  • 9
  • 1
  • paste that code in here, please. Remove the link to the image – Andrew Tobilko Jan 10 '18 at 13:32
  • It is being set in a static initialisation block. – christopher Jan 10 '18 at 13:34
  • 2
    `unsafe=;` shouldn't compile – Andrew Tobilko Jan 10 '18 at 13:38
  • 1
    from the `AtomicBoolean` class: `private static final Unsafe unsafe = Unsafe.getUnsafe();` – Andrew Tobilko Jan 10 '18 at 13:38
  • 1
    Considering there is a typo in the class name, I guess this is not the real code from the JDK. Edit: just saw the previous screenshot. Maybe the IDE displays it wrong or messed with the code. I just checked the sources of a few versions and they all use the fully qualified name of Serializable and have different field order (eg. value comes after the static initializer) – kapex Jan 10 '18 at 13:47
  • 1
    Just in case someone does not want to dig in the history: the [screenshot](https://i.stack.imgur.com/6TEZP.png) from some IDE is really missing ```Unsafe.getUnsafe()```, which is actually present in the JDK. – tevemadar Jan 10 '18 at 14:06

0 Answers0