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
8
votes
3 answers

Autocomplete for generic types in Eclipse

"Refer to objects by their interfaces" is a good practise, as mentioned in Effective Java. So for example i prefer List al = new ArrayList(); over ArrayList al = new ArrayList(); in my code. One annoying thing is…
AvrDragon
  • 7,139
  • 4
  • 27
  • 42
7
votes
1 answer

Canonical form of field

I'm studying Effective Java, Item 8 (Obey the general contract when overriding equals). It has been explained quite clearly by the author, but still some parts are not that much elaborated. For this example, he considers a class…
gaurav jain
  • 3,119
  • 3
  • 31
  • 48
7
votes
1 answer

Equals method in Joshua Bloch's Effective Java

Please look at this link of Joshua Bloch's Effective Java. In second paragraph, the author says: The class is private or package-private, and you are certain that its equals method will never be invoked. Arguably, the equals method should be…
user961690
  • 698
  • 1
  • 11
  • 22
7
votes
2 answers

Forwarding Class Example

Reading Effective Java, I saw the following example from Item 16: Favor composition over inheritance. In the below InstrumentedSet, the book shows we can keep track of how many times an element was inserted (via the InstrumentedSet.addCount…
Kevin Meredith
  • 41,036
  • 63
  • 209
  • 384
7
votes
2 answers

Builder Pattern: which variant is preferred?

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…
Sudhakar
  • 4,823
  • 2
  • 35
  • 42
7
votes
3 answers

Why is it safe to suppress this unchecked warning?

Consider the UnaryFunction interface defined in Effective Java generics chapter . public interface UnaryFunction { T apply(T arg); } and the following code for returning the UnaryFunction // Generic singleton factory pattern private static…
Geek
  • 26,489
  • 43
  • 149
  • 227
7
votes
3 answers

Adapting the Builder pattern for method invocation

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…
Ashutosh Jindal
  • 18,501
  • 4
  • 62
  • 91
6
votes
3 answers

Using auto generated id of Hibernate entity object in the equals and hashcode methods

Lovely equals and hashcode, all the theory is here and also here I have taken the decision to use the auto-generated id within equals() and hashcode() in a number of my hibernate entity/domain objects. However, a number of websites say you should…
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
6
votes
2 answers

classes managing own memory

Effective Java: Item 6: Eliminate obsolete object references. Generally speaking, whenever a class manages its own memory, the programmer should be alert for memory leaks. Whenever an element is freed, any object references contained in…
Atul
  • 2,673
  • 2
  • 28
  • 34
6
votes
3 answers

Effective Java: Safety of Forwarding Classes

Effective Java 3rd Edition, Item 18: Favor composition over inheritance describes an issue with using inheritance to add behavior to a class: A related cause of fragility in subclasses is that their superclass can acquire new methods in subsequent…
shmosel
  • 49,289
  • 6
  • 73
  • 138
6
votes
3 answers

Java Serialization

I posting a doubt that I came across reading Effective Java. I apologize if its a real simple and straight forward doubt. So in Item 74 - Implement Serializable judiciously, He is saying that even after implementing a good Information Hiding on your…
t0mcat
  • 5,511
  • 19
  • 45
  • 58
6
votes
2 answers

Enum Types as explained in Effective Java by Joshua Bloch

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…
Ankit
  • 251
  • 2
  • 9
6
votes
4 answers

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

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…
Kyle
  • 379
  • 6
  • 17
6
votes
6 answers

Why is there a need to override hashcode if I override the 'equals' method in Java?

I know there is a need to override hashcode whenever the equals method is overridden in Java. That is merely a contract. I am trying to understand the logic behind this. I was reading *Effective Java by Joshua Bloch, and I came across this code…
brain storm
  • 30,124
  • 69
  • 225
  • 393
6
votes
2 answers

Static factory methods in effective java

In Effective Java , Item 1, it says that the static factory methods made the Collections framework much smaller than it would have been. Could someone please explain how ? I can't understand how the following is possible just because of using static…
Prasad Weera
  • 1,223
  • 3
  • 13
  • 25
1 2
3
11 12