public class Main {
static void times2(int n) {
n *= 2;
}
public static void main(String... args) {
int n = 1;
times2(n);
System.out.println(n);
}
}
public class Main {
int n = 1;
static void times2(Main obj) {
obj.n *= 2;
}
public static void main(String... args) {
Main obj = new Main();
times2(obj);
System.out.println(obj.n);
}
}
public class Main {
int n = 1;
static void times2(Main obj) {
obj = new Main();
obj.n *= 2;
}
public static void main(String... args) {
Main obj = new Main();
times2(obj);
System.out.println(obj.n);
}
}
public class Triangle {
private Sides[] sides = new Sides[3];
public Triangle() {
}
public Side getA() { return sides[0]; }
public Side getB() { return sides[1]; }
public Side getC() { return sides[2]; }
public void setA(Side a) { this.a = sides[0]; }
public void setB(Side b) { this.b = sides[1]; }
public void setC(Side c) { this.c = sides[2]; }
public boolean equals(Object obj) {
if (obj == null) { return false; }
if (obj == this) { return true; }
if (!(obj instanceof Triangle)) { return false; }
return Arrays.equals(sides, ((Triangle) obj).sides));
}
}
@Entity
@Table(name = "foos")
public class Foo implements java.io.Serializable {
private String code;
// remember this is unnecessary if there is no other constructor
public Foo(){}
@Column(name = "CODE")
public String getCode() {
return this.code;
}
public void setSCode(String Code) {
this.code = code;
}
}
public class Foo {
private final Date date;
public Foo(Date date) {
this.date = date; // DON'T
}
// more code
}
public class Foo {
private final Date date;
public Foo(Date date) {
this.date = new Date(date.getTime()); // DO
}
// more code
}
public class Foo {
private final LocalDateTime date;
public Foo(LocalDateTime date) {
this.date = date; // Is this OK?
}
// more code
}
public class Bar {
private List<String> xyz;
// code
public List<String> getXyz() {
return xyz; // DON'T
}
}
public class Bar {
private List<String> xyz;
// code
public List<String> getXyz() {
return new ArrayList<>(xyz); // DO
}
}
public class Bar {
private List<String> xyz;
// code
public List<String> getXyz() {
return Collection.ummodifiableList(xyz); // DO
}
}
Make class final
public final class Fraction {
public final int denominator;
public final int numerator;
public Fraction(int d, int n) {
final int GCF = gcf(d, n);
this.denominator = d / GCF;
this.numerator = n / GCF;
}
public Fraction plus(final Fraction that) {
final int LCM = lcm(numerator, that.numerator);
final int denominator = LCM / this.numerator * this.denominator
+ LCM / that.numerator * that.denominator;
return new Fraction(denominator, LCM);
}
}
public Fraction plus(final Fraction that) {
final int LCM = lcm(numerator, that.numerator);
final int denominator = LCM / this.numerator * this.denominator
+ LCM / that.numerator * that.denominator;
return new Fraction(denominator, LCM);
}
public Fraction opposite() {
return new Fraction(-denominator, numerator);
}
public Fraction minus(final Fraction that) {
return this.plus(that.opposite());
}
public enum Cell {
ALIVE {
public Cell next(int aliveNeighbors) {
if (aliveNeighbors < 2 || aliveNeighbors > 3) {
return DEAD;
}
return this;
}
},
DEAD {
public Cell next(int aliveNeighbors) {
if (aliveNeighbors == 3) {
return ALIVE;
}
return this;
}
};
public abstract Cell next(int aliveNeighbors);
}
public final class Document {
private final String title;
private final String text;
private final LocalDate dateOfPublish;
// more code
public Document withTitle(String newTitle) {
return new Document(newTitle, this.text, this.dateOfPublish);
}
}
LocalDateTime thePast = timePoint
.withDayOfMonth(10)
.withYear(2010);
public final class Board {
private final Cell[][] board;
private final int columns;
private final int rows;
private Board(Builder builder) {
board = builder.board;
columns = builder.columns;
rows = builder.rows;
}
public static class Builder {
private Cell[][] board;
private int columns;
private int rows;
public Builder(int columns, int rows) {
this.columns = columns;
this.rows = rows;
board = new Cell[columns][rows];
for (int i = 0; i < columns; ++i) {
Arrays.fill(board[i], Cell.DEAD);
}
}
public Builder aliveCellIn(int column, int row) {
board[column][row] = Cell.ALIVE;
return this;
}
public Board build() {
return new Board(this);
}
}
}
Board board = new Board.Builder(4, 4)
.aliveCellIn(1, 1)
.aliveCellIn(1, 2)
.aliveCellIn(2, 1)
.aliveCellIn(2, 2)
.build();
public final class Board {
private final Cell[][] board;
private final int columns;
private final int rows;
// Code
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < columns; ++i) {
for (int j = 0; j < rows; ++j) {
builder.append(board[i][j]);
}
builder.append(String.format("%n"));
}
return builder.toString();
}
}
public final class Board {
private final Cell[][] board;
private final int columns;
private final int rows;
private static final int[] dx = {-1,-1,-1, 0, 0, 1, 1, 1};
private static final int[] dy = {-1, 0, 1,-1, 1,-1, 0, 1};
private static final int NEIGHBORS = dx.length;
private Board(Board copy) {
columns = copy.columns;
rows = copy.rows;
this.board = new Cell[columns][rows];
for (int i = 0; i < columns; ++i) {
this.board[i] = Arrays.copyOf(copy.board[i], rows);
}
}
// Code
}
public final class Board {
// Code
private boolean valid(int column, int row) {
return column >= 0 && column < columns && row >= 0 && row < rows;
}
private int countAliveNeighbors(int column, int row) {
int total = 0;
for (int i = 0; i < NEIGHBORS; ++i) {
int nx = column + dx[i];
int ny = row + dy[i];
if (valid(nx, ny)) {
total += board[nx][ny] == Cell.ALIVE ? 1 : 0;
}
}
return total;
}
public Board next() {
Board copy = new Board(this);
for (int i = 0; i < columns; ++i) {
for (int j = 0; j < rows; ++j) {
int alive = countAliveNeighbors(i, j);
copy.board[i][j] = board[i][j].next(alive);
}
}
return copy;
}
// Code
}
public final class Foo {
private final List<String> list;
public Foo(final List<String> list) {
this.list = new ArrayList<>(Objects.requireNonNull(list));
}
public List<String> getList() {
return Collections.unmodifiableList(list);
}
}
public final class Document {
private final String title;
private final String document;
private final String text;
public Document(final String title, final String document, final String text) {
this.title = Objects.requireNonNull(title);
this.document = Objects.requireNonNull(document);
this.text = Objects.requireNonNull(text);
}
}