effective-java

Class.asSubclass signature

核能气质少年 提交于 2019-12-03 15:33:26
问题 my question is quite theoretic... This is the signature of Class.asSubclass (Javadoc): public <U> Class<? extends U> asSubclass(Class<U> clazz) Why are wildcard generics used in the return type? From my understanding of generics a better signature could be: public <U> Class<U> asSubclass(Class<U> clazz) because you can for sure cast Class<? extends U> to a more simple Class<U> Bloch in his book "Effective Java" recommends (page 137, item 28): Do not use wildcard types as return types. Rather

Why is the volatile field copied to a local variable when doing double check locking

别来无恙 提交于 2019-12-03 13:40:55
问题 I am reading about double check locking from Effective Java . The code does the following: private volatile FieldType field; FieldType getField() { FieldType result = field; if (result == null) { // First check (no locking) synchronized(this) { result = field; if (result == null) // Second check (with locking) field = result = computeFieldValue(); } } return result; } It says that using result seems unneeded but actually ensures that the field is only read only once in the common case where

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

送分小仙女□ 提交于 2019-12-03 12:20:59
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 inference Disadvantages: Private classes cannot be subclassed They do not stand out in the documentation as

Java: When to add readObjectNoData() during serialization?

若如初见. 提交于 2019-12-02 22:12:51
I am reading the serialization chapter in "Effective Java". I am trying to understand the below paragraph in the book. If you implement a class with instance fields that is serializable and extendable,there is a caution you should be aware of. If the class has invariants that would be violated if its instance fields were initialized to their default values (zero for integral types, false for boolean, and null for object reference types), you must add this readObjectNoData method to the class: // readObjectNoData for stateful extendable serializable classes private void readObjectNoData()

Understanding the concept behind Service provider framework like JDBC using the factory method

*爱你&永不变心* 提交于 2019-12-02 15:38:53
From Effective Java ( Item 1 : Consider static factory methods instead of constructors ): The class of the object returned by a static factory method need not even exist at the time the class containing the method is written. Such flexible static factory methods form the basis of service provider frameworks, such as the Java Database Connectivity API (JDBC). A service provider framework is a system in which multiple service providers implement a service, and the system makes the implementations available to its clients, decoupling them from the implementations. I specifically do not understand

Java: Why shouldn't clone() be used for defensive copying?

雨燕双飞 提交于 2019-12-01 20:03:42
问题 In Effective Java (Chapter 7), it says Note also that we did not use Date’s clone method to make the defensive copies. Because Date is nonfinal, the clone method is not guaranteed to return an object whose class is java.util.Date: it could return an instance of an untrusted subclass specifically designed for malicious mischief. Such a subclass could, for example, record a reference to each instance in a private static list at the time of its creation and allow the attacker to access this list

Java: Why shouldn't clone() be used for defensive copying?

喜夏-厌秋 提交于 2019-12-01 18:48:10
In Effective Java (Chapter 7), it says Note also that we did not use Date’s clone method to make the defensive copies. Because Date is nonfinal, the clone method is not guaranteed to return an object whose class is java.util.Date: it could return an instance of an untrusted subclass specifically designed for malicious mischief. Such a subclass could, for example, record a reference to each instance in a private static list at the time of its creation and allow the attacker to access this list. This would give the attacker free reign over all instances. To prevent this sort of attack, do not

How do I make defensive copy of an object?

北战南征 提交于 2019-12-01 09:09:09
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. What you need to do is in MutableObject return_immutable_field() { return immutable_field; } Change to: MutableObject return_immutable_field

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

萝らか妹 提交于 2019-12-01 02:47:39
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; Suppose you have two files: Foo.java: public class Foo { public static final int SOMETHING = 1; } Bar.java: public class Bar { public static void main(String[] args) { System.out.println(Foo.SOMETHING); } } Compile

Builder Pattern: which variant is preferred? [closed]

匆匆过客 提交于 2019-11-30 14:25:08
I was going through Effective Java book , and creating notes for my future reference , i came across Builder Pattern. Well i understood what it is and how its suppose to be used.In the process i created a two example variations of the builder pattern. I would need help in listing down the differences and the advantage each has? Well i certainly noticed that , Example 1 exposes less methods , there by less restrictive and more generic , there by allowing it to be used more flexibly. Please point out other things i have missed? Example 1 package item2; /** * @author Sudhakar Duraiswamy * */