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
4
votes
2 answers

Definition of a constant field in Bloch's Effective Java 2nd edition

Quote: If a static final field has a mutable reference type, it can still be a constant field if the referenced object is immutable. I'm not sure what this mean; can someone give an example of that?
traveh
  • 2,700
  • 3
  • 27
  • 44
4
votes
1 answer

Builder pattern multiple varargs

I'm reading up Joshua Bloch's 'Effective Java' where in Item 2, he mentions the advantages of using Builder pattern when dealing with several parameters in the constructor. All's good, until I saw the multiple var-args difference between the…
gaurav jain
  • 3,119
  • 3
  • 31
  • 48
4
votes
3 answers

Is this class fully Immutable?

I am trying to convert a mutable class to an Immutable class following the advices given in Effective Java Item 15 (Minimize Mutability). Can anybody tell me whether the class I created is fully immutable or not? Mutable Class public class Record…
Shekhar
  • 5,771
  • 10
  • 42
  • 48
4
votes
2 answers

Excluding concurrent activity from a concurrent Java collection

Joshua Bloch's Effective Java, Second Edition, item 69 , states that [...] To provide high concurrency, these implementations manage their own synchronization internally (Item 67). Therefore, it is impossible to exclude concurrent activity from …
veryltdbeard
  • 320
  • 4
  • 13
4
votes
2 answers

Java - Generics - Explicit casting and cast method of Class "Class"

Why does using the cast method on the Class Class produce an unchecked warning at compile time? If you peek inside the cast method, you found this code : public T cast(Object obj) { if (obj != null && !isInstance(obj)) throw new…
Victor
  • 3,841
  • 2
  • 37
  • 63
4
votes
2 answers

JavaBean disadvantage - inconsistent during construction

Effective Java - Item-2 states , a JavaBean may be in an inconsistent state partway through its construction. I could not understand this, If an object is being constructed in a method, how would that go inconsistent, if exception has to occur,…
codingenious
  • 8,385
  • 12
  • 60
  • 90
4
votes
3 answers

What is the most effective way of writing a factory method?

In most of the cases when we write a factory method it is a bunch of if conditions which can keep on growing. What is the most effective way of writing such a method (with the least if conditions)? public A createA(final String id) { if…
4
votes
1 answer

Effective Java Item 9, is the CaseInsensitiveString example correct?

I'm reading the second edition of the book, page 36. I don't understand the solution to the simmetry problem: @override public boolean equals(Object o) { return o instanceof CaseInsensitiveString && ((CaseInsensitiveString)…
CptWasp
  • 459
  • 2
  • 13
4
votes
4 answers

Should you avoid Guavas Ordering.usingToString()?

This question was prompted after reading Joshua Bloch's "Effective Java". Specifically in Item #10, he argues that it is bad practice to parse an object's string representation and use it for anything except a friendlier printout/debug. The reason…
m2o
  • 6,475
  • 6
  • 27
  • 24
4
votes
3 answers

What is a Scalar type and why is it riskier to suppress an unchecked cast to an array type than to a scalar type?

From Effective Java Item 26 Favour Generic types All other things being equal, it is riskier to suppress an unchecked cast to an array type than to a scalar type, which would suggest the second solution. But in a more realistic generic class…
Geek
  • 26,489
  • 43
  • 149
  • 227
4
votes
4 answers

What does it mean to say that a list is locked internally?

This code is from the book Effective Java Object[] snapshot = list.toArray();// Locks list internally I am mainly interested in the comment here . Does it make the list unmodifiable ? What does it mean to say that a list is locked internally ?…
Geek
  • 26,489
  • 43
  • 149
  • 227
4
votes
3 answers

How to use a single builder to build multiple objects?

This one is straight from Effective java 2. I'm not sure what does this statement from Item 2 means The Builder pattern is flexible. A single builder can be used to build multiple objects. The parameters of the builder can be tweaked between …
tintin
  • 5,676
  • 15
  • 68
  • 97
4
votes
3 answers

Need I make all classes immutable?

I read Effective Java, and there written If a class cannot be made immutable, limit its mutability as much as possible... and ...make every field final unless there is a compelling reason to make it nonfinal. So need I always make all my…
WelcomeTo
  • 19,843
  • 53
  • 170
  • 286
3
votes
4 answers

Implementing hashcode() in java

In the guidelines to write a good hashCode() written in Effective java, the author mentions the following step if the field is long. If the field is a long, compute (int) (f ^ (f >>> 32)). I am not able to get why this is done. Why are we doing…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
3
votes
3 answers

java : Function objects as strategies

I am reading Effective Java. In a section that talks about using function objects as strategies, the below paragraph is present. Because the strategy interface serves as a type for all of its concrete strategy instances, a concrete strategy…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130