You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
53 lines
1.2 KiB
53 lines
1.2 KiB
8 years ago
|
/*
|
||
|
* Copyright (c) 2016 - present Facebook, Inc.
|
||
|
* All rights reserved.
|
||
|
*
|
||
|
* This source code is licensed under the BSD style license found in the
|
||
|
* LICENSE file in the root directory of this source tree. An additional grant
|
||
|
* of patent rights can be found in the PATENTS file in the same directory.
|
||
|
*/
|
||
|
|
||
|
import javax.annotation.concurrent.ThreadSafe;
|
||
|
|
||
|
@ThreadSafe
|
||
|
public class Constructors {
|
||
|
int field;
|
||
|
static Object staticField;
|
||
|
|
||
|
public Constructors(int i) {
|
||
|
field = i; // ok
|
||
|
}
|
||
|
|
||
|
public Constructors() {
|
||
|
staticField = new Object(); // not ok;
|
||
|
}
|
||
|
|
||
|
private Constructors(Object o) {
|
||
|
staticField = o; // ok because this is private
|
||
|
}
|
||
|
|
||
|
public Constructors(Constructors o) {
|
||
|
o.field = 42; // not ok
|
||
|
}
|
||
|
|
||
8 years ago
|
public Constructors(String s) {
|
||
|
calledFromConstructorOk(); // ok
|
||
|
}
|
||
|
|
||
|
private void calledFromConstructorOk() {
|
||
|
this.field = 7;
|
||
|
}
|
||
|
|
||
8 years ago
|
public static synchronized Constructors singletonOk() {
|
||
|
// ok because lock is held during write to static field in constructor
|
||
|
return new Constructors(new Object());
|
||
|
}
|
||
|
|
||
|
public static Constructors singletonBad() {
|
||
|
// not ok because no lock is held
|
||
|
return new Constructors(new Object());
|
||
|
}
|
||
|
|
||
|
|
||
|
}
|