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
1
vote
1 answer

Understanding the creation of a non static member class in Java as described in Effective Java book

Following is from Effective Java: The association between a nonstatic member class instance and its enclosing instance is established when the former is created; it cannot be modified thereafter. Normally, the association is established…
Geek
  • 26,489
  • 43
  • 149
  • 227
0
votes
3 answers

Why immutable new String("fish") != new String("fish")?

I remember reading a section, possibly in Bloch's Effective Java, that said that for most cases, where String a = "fish"; String b = "fish"; that a == b in most cases because Strings are immutable. But that due to temporary construction of objects…
orbfish
  • 7,381
  • 14
  • 58
  • 75
0
votes
1 answer

What is the benefits to use assertion in Java's private methods

I'm reading Effective Java Item 49. I am a bit confused about the example using assertion to validate params in private methods. Some posts explained why we can use assertions in private because public methods are responsible for ensuring that the…
hounan
  • 19
  • 5
0
votes
2 answers

nested static builder class, further explanation on its plumbing

public class MyPojo{ String required; Integer optionalOne; Integer optionalTwo; private MyPojo(Builder builder){ this.required = builder.required this.optionalOne = builder.one; this.optionalTwo =…
NimChimpsky
  • 46,453
  • 60
  • 198
  • 311
0
votes
1 answer

What is an immutable value class in Java? (Effective Java by Joshua Bloch)

I'm currently diving deep (at least relative to my limited knowledge as a programmer) into Java using Joshua Bloch's Effective Java. In Item 1 under Creating and Destroying Objects, he states to consider static factory methods instead of…
Rahul
  • 543
  • 1
  • 7
  • 24
0
votes
2 answers

Inheritance vs Composition: Does composition effectively solve dependency issues? [Effective Java]

I'm semi-familiar with Java and came across something in Effective Java(2017) that didn't make much sense to me. Below is a piece from the book. (item 18) Unlike method invocation, inheritance violates encapsulation. In other words, a subclass…
rivid
  • 1
  • 3
0
votes
1 answer

Question about "heap pollution" at solution#1 in "Effective Java Item#29 Favor Generic Types"

I am reading Effective Java Item#29 Favor Generic Types. I'm confused about heap pollution that this item mentioned when it talked about the reason to choose solution#1 or solution#2. Both techniques for eliminating the generic array creation have…
Willy
  • 3
  • 3
0
votes
1 answer

Did Joshua Bloch say not to use singleton with underlying resources?

I have been reading Joshua Bloch's Effective Java book. In item #5, he seems to say not to use a singleton or static utility class to implement a class that depends on one or more underlying resources, and do not have the class create these…
0
votes
1 answer

how do I log crash to the server without try-catch block?

I do not understand when to use try-catch , and what is wrong with them ,special this item (57 java-effective) could someone explain this // Horrible abuse of exceptions. Don't ever do this! try { int i = 0; while(true) …
العبد
  • 359
  • 5
  • 15
0
votes
1 answer

Java Generics Cast required Compile Time Error

public class ChooserVer2 { private final T[] choice; public ChooserVer2(Collection collection) { choice = (T[])collection.toArray(); } public T randomObj() { Random rnd = ThreadLocalRandom.current(); …
Pritpal
  • 571
  • 1
  • 6
  • 11
0
votes
0 answers

Are Java concurrent collections performance tips documented anywhere (e.g., for ConcurrentHashMap, calling `get()` before `putIfAbsent()`)

Gist In Effective Java (Third Edition), the author provided a performance tip in relation to ConcurrentHashMap---i.e., when using existingValue = map.putIfAbsent(key, value), consider first calling existingValue = map.get(key) and skipping…
0
votes
1 answer

Why is this set containment check failing?

I am going through the Effective Java 3rd edition and I was reading Item 10: Follow Equals contract when overriding. There is an example there which I was trying to simulate on my machine. Below is the code for the same. public class Point { …
Pritpal
  • 571
  • 1
  • 6
  • 11
0
votes
3 answers

Static factory method does not work

Started reading "Effective java" and can't understand why it doesn't work for me when I try coding an example.. Compile error: Error:(12, 16) java: constructor Car in class Car cannot be applied to given types; public class Car { String…
0
votes
2 answers

Remove the Builder Class of the Builder Pattern in Effective Java

I have recently read Effective Java and I found that the Builder Pattern (item #2) is very interesting. However, I have a question: why should we create a static builder when we could do this: // JavaBeans Pattern public class NutritionFacts…
nguyentt
  • 649
  • 6
  • 13
0
votes
2 answers

Effective Java Item1 - Static factory method for object creation

I was going through Effective java item 1, where "Static factory method vs constructors" for object creation is discussed. One of the disadvantages mentioned is the following: "The main disadvantage of providing only static factory methods is that…