effective-java

Do we ever need to prefer constructors over static factory methods? If so, when?

大兔子大兔子 提交于 2019-12-21 03:44:16
问题 I have been reading Effective Java by Joshua Bloch and so far it really lives up to its reputation. The very first item makes a convincing case for static factory methods over constructors . So much that I began to question the validity of the good old constructors :). The advantages/disadvantages from the book are summarized below: Advantages: They have names! We have total instance control (Singletons, performance, etc.) They can return a subtype/interface Compiler can provide type

Why are public static final array a security hole?

半城伤御伤魂 提交于 2019-12-17 22:22:29
问题 Effective java says: // Potential security hole! static public final Thing[] VALUES = { ... }; Can somebody tell me what is the security hole? 回答1: Declaring static final public fields is usually the hallmark of a class constant. It's perfectly fine for primitive types (ints, doubles etc..), and immutable classes, like strings and java.awt.Color . With arrays, the problem is that even though the array reference is constant, the elements of the array can still be changed, and as it's a field,

Effective Java By Joshua Bloch: Item1 - Static Factory Method

余生长醉 提交于 2019-12-17 21:52:37
问题 I am reading the Effective Java by Joshua Bloch and I have question about Item1 Static Factory Method . Quote[Bloch, p.7] Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in non-instantiable class named Types. For example, the Java Collections Framework, provide unmodifiable collections, synchronized collections, and the like. Nearly all of these implementations are export via static factory methods in one noninstantiable class

What's the difference between raw types, unbounded wild cards and using Object in generics

余生长醉 提交于 2019-12-17 06:37:23
问题 I am reading the chapter on Generics in Effective Java. Help me understand difference between Set , Set<?> and Set<Object> ? The following paragraph is taken from the book. As a quick review, Set<Object> is a parameterized type representing a set that can contain objects of any type, Set<?> is a wildcard type representing a set that can contain only objects of some unknown type, and Set is a raw type, which opts out of the generic type system. What is meant by "some unknown type"? Are all

Adapting the Builder pattern for method invocation

一笑奈何 提交于 2019-12-09 12:44:59
问题 This is an attempt to understand a portion of ITEM 40: Design Method Signatures Carefully from Effective Java 2nd Edition. One of the things suggested to improve method signature readability is to aim for four or fewer parameters. It is suggested that longer parameter lists be managed by using a variety of techniques one of which is as follows : A third technique that combines aspects of the first two is to adapt the Builder pattern (Item 2) from object construction to method invocation. If

What does it mean to say that int enum patterns are compile-time constants?

╄→гoц情女王★ 提交于 2019-12-09 02:33:07
问题 This is from Effective Java Programs that use the int enum pattern are brittle. Because int enums are compile-time constants, they are compiled into the clients that use them. Can some one explain why the int enum pattern is called compiled type constant and what is meant by compiled into the clients ? Here s' an example of such a constant : public static final int APPLE_FUJI = 0; 回答1: Suppose you have two files: Foo.java: public class Foo { public static final int SOMETHING = 1; } Bar.java:

Do we need a .build() method in the Builder Pattern?

守給你的承諾、 提交于 2019-12-08 16:27:39
问题 I had a question regarding the "Builder Pattern" covered in "Effective Java". Do we need a .build() method for it to correctly implement the pattern? For instance, let's say that we have the following class: public class CoffeeDrink { private int numEspressoShots; private short milkType; private boolean withWhip; private CoffeeDrink() { } public static CoffeeDrink buildNewDrink() { return new CoffeeDrink(); } public CoffeeDrink withEspresso(int n) { this.numEspressoShots = n; return this; }

Effective Java Item 11: Override clone Judiciously

眉间皱痕 提交于 2019-12-06 01:26:49
For a class with an array field Josh says if the clone method merely return super.clone(), the resulting class instance will have the correct values in primitive fields, but its array field will refer to the same array as the original class instance. Modifying the original will destroy the invariants and vice-versa. He used the example of custom Stack implementation, I am using a simple Student class class Student implements Cloneable { private String name; private int age; private int[] marks = {90, 70, 80}; public void setName(String name) { this.name = name; } public void setAge(int age) {

Enum Types as explained in Effective Java by Joshua Bloch

隐身守侯 提交于 2019-12-04 09:16:12
Please see this link . Regarding Enums, Mr. Bloch says Java’s enum types are classes that export one instance for each enumeration constant via a public static final field. I read the Enum Class documentation but there was no public static final field , then how does the above statement hold true. Please explain. Thanks Create a Test.java file and write Test enum : public enum Test { Hello } compile this class: javac Test.java ,and use javap Test to get the compiled class: public final class Test extends java.lang.Enum{ public static final Test Hello; public static Test[] values(); public

How do I make defensive copy of an object?

偶尔善良 提交于 2019-12-04 01:46:56
问题 How do I make defensive copies of a Mutable Object which contains a mutable field in an Immutable Object? class ImmutableObject { private final MutableObject immutable_field; ImmutableObject(MutableObject y) { this.immutable_field = y; } } class MutableObject { public int mutable_field; } The MutableObject does not have a constructor that lets me set the field. The MutableObject's current state should be captured in the Immutable Object and never changed. 回答1: What you need to do is in