Questions tagged [effective-java]

Effective Java is a book by Joshua Bloch "designed to help Java programmers make the most effective use of the Java programming language and its fundamental libraries"

Effective Java is a book by Joshua Bloch of patterns "designed to help Java programmers make the most effective use of the Java programming language and its fundamental libraries".

178 questions
3
votes
7 answers

Immutable in java

In Effective Java, Bloch recommends to make all the fields final in making an object immutable . Is it necessary to do so ? Won't just not giving accessor methods make it immutable. For example class A { private int x; A (int x) { …
user2434
  • 6,339
  • 18
  • 63
  • 87
3
votes
4 answers

Joshua Bloch Item #1 Static Factory Methods Instead of Constructors - Object creation

Source of Question I was wondering about the following advantage of Static Factory Methods described by Joshua Blochs "Effective Java", 3rd edition in item #1: A second advantage of static factory methods is that, unlike constructors, they are not…
3
votes
0 answers

What dose mean additional stipulation for add, equals, hashcode method of Set interface in java

In Effective Java 3/E Item 41, Marker Annotation I saw the below sentence Arguably, the Set interface is just such a restricted marker interface. It is applicable only to Collection subtypes, but it adds no methods beyond those defined by…
Bebop
  • 61
  • 4
3
votes
2 answers

What is the difference between a Singleton and static factory methods

I want to know if both singleton and static factory method create only one instance then why there are two concepts for same purpose? Note: Here term "static factory method" is taken from Effective java book by Joshua bloch where he has written: "A…
3
votes
1 answer

Grouping Related Constants Shared Between Classes

In Effective Java, Item 17, Josh Bloch argues that putting static members into an interface (and implementing that interface) is a bad practice known as the Constant Interface Antipattern: The constant interface pattern is a poor use of interfaces.…
Tom Tresansky
  • 19,364
  • 17
  • 93
  • 129
3
votes
2 answers

How do I make defensive copy of an object?

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; …
unj2
  • 52,135
  • 87
  • 247
  • 375
3
votes
2 answers

Effective Java Item 11: Override clone Judiciously

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…
EMM
  • 1,812
  • 8
  • 36
  • 58
3
votes
4 answers

Effective Java - Item 25 - Generics class cast exception Mixing List and Arrays

I'm reading Effective Java 2nd edition from Joshua Bloch, item 25 (page 122). As you read further into the chapter, you get to a point where the author writes the following code : // Naive generic version of reduction - won't compile! static E…
Victor
  • 3,841
  • 2
  • 37
  • 63
3
votes
2 answers

why did Joshua Bloch decrement the "size" value of stack in the pop method in effective java?

here is the code from Item 6, pg 24, chapter 2 of effective java 2nd edition, by Joshua Bloch. In the pop method he defines, he uses elements[--size]. I am wondering why he used --size, instead elements[size--] should return the same correct? public…
brain storm
  • 30,124
  • 69
  • 225
  • 393
3
votes
4 answers

A way to extend an instantiable class and add a value component while preserving the equals contract

Effective Java by Joshua Blotch states: There is no way to extend an instantiable class and add a value component while preserving the equals contract, unless you are willing to forgo the benefits of object-oriented abstraction. Effective Java…
johnchen902
  • 9,531
  • 1
  • 27
  • 69
2
votes
3 answers

Multiple threads reading a variable

I'm reading "Effective Java Second Edition" by Joshua Bloch and im confused by below statement with regard to concurrency - "The language specification gaurantees that reading or writing a variable is atomic unless the variable is of type long or…
blue-sky
  • 51,962
  • 152
  • 427
  • 752
2
votes
4 answers

Java : Questions on clone method

I am reading Effective Java and the book has the below comment on the clone method. In practice, a class that implements Cloneable is expected to provide a properly functioning public clone method. It is not, in general, possible to do so…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
2
votes
1 answer

Example of an elegant implementation of 'Defensive Copying' of a nullable java.util.Date in a JavaBean's getter/setter implementation?

Is there an elegant Java implementation of Joshua Bloch's defensive copying techniques using the example below? The nullChecking is really the issue I think, but perhaps there is a much simpler way to achieve defensive copying. public class…
Brian
  • 13,412
  • 10
  • 56
  • 82
2
votes
1 answer

Why do Java finalizers have security problems?

I'm reading Effective Java by Joshua Bloch. In ITEM 8: AVOID FINALIZERS AND CLEANERS of CHAPTER 2 he states: Finalizers have a serious security problem: they open your class up to finalizer attacks.The idea behind a finalizer attack is simple: If…
2
votes
1 answer

Effective in Java Item 89: For instance control, prefer enum types to readResolve - Why can the stealer change the field of the original instance?

A serializable class is defined as public class Elvis implements Serializable { public static final Elvis INSTANCE = new Elvis(); private Elvis() { } private String[] favoriteSongs ={ "Hound Dog", "Heartbreak Hotel" }; public void…
Michael Tsai
  • 751
  • 2
  • 11
  • 21