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"
178
questions
4
votes
4
answers
149
views
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 ...
0
votes
1
answer
143
views
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 ...
3
votes
1
answer
408
views
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....
0
votes
1
answer
132
views
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 ...
2
votes
1
answer
125
views
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 ={ &...
1
vote
1
answer
78
views
Why is it a good practice to make a final field pointing to an immutable obj non public?
effective java has a statement about the access control:
Even if a field is final and refers to an immutable object, by making it public you give up the flexibility to switch to a new internal data ...
0
votes
2
answers
576
views
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 ...
0
votes
1
answer
82
views
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 ...
0
votes
1
answer
305
views
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,...
2
votes
3
answers
295
views
It''s never a good idea for a public class to expose fields directly, but why it is less harmful if the fields are immutable?
I'm reading an article from Effective Java Item 14 - In public classes, use accessor methods, not public fields. In the book it says: While it’s never a good idea for a public class to expose fields ...
12
votes
2
answers
424
views
Why did Joshua Bloch use 2*size + 1 for resizing the stack in Effective Java?
This is the code I am talking about:
public class Stack {
private Object[] elements;
private int size = 0;
private static final int DEFAULT_INITIAL_CAPACITY = 16;
public Stack() {
elements ...
1
vote
2
answers
163
views
Glacier Class Immutable Checker does not works on Checker Framework 3.6.0
I try to use Glacier Class Immutable Checker does not works on Checker Framework 3.6.0.
I get Glacier 0.1 with Maven.
(http://mcoblenz.github.io/Glacier/)
Then, I tried to check the sample source, but ...
0
votes
1
answer
50
views
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 {
...
-2
votes
2
answers
103
views
What do you thínk about Java designers decided java.lang.String.valueOf((Object)null) returns "null" but not "" [closed]
[edited this question at 2020/07/27 01:30 UTC]
What do you thínk about Java designers decided java.lang.String.valueOf((Object)null) returns "null" but not "" which is the length 0 ...
0
votes
1
answer
145
views
Java Generics Cast required Compile Time Error
public class ChooserVer2<T> {
private final T[] choice;
public ChooserVer2(Collection<T> collection) {
choice = (T[])collection.toArray();
}
public T randomObj() {...
0
votes
0
answers
50
views
Are Java concurrent collections performance tips documented anywhere (e.g., for ConcurrentHashMap, calling `get()` before `putIfAbsent()`) [duplicate]
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 ...
2
votes
1
answer
166
views
Effective Java Item 44 - Where is the `DoubleToObjFunction` in the standard library?
Background
In Effective Java at Item 44, the author recommended the use of standard functional interfaces (instead of defining custom ones). In relation to java.util.function package, the author:
...
0
votes
1
answer
39
views
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 ...
3
votes
3
answers
1k
views
Public methods vs public APIs
In clean code book, there is a point that says "Javadocs in Public APIs".
And the same for Effective java book, there is this item :
"Item 56: Write doc comments for all exposed API elements".
So ...
3
votes
0
answers
62
views
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 ...
3
votes
3
answers
2k
views
Typesafe heterogeneous containers involving generics
I need to create a container that provides a way for me to store elements of generic type, kind of like this effective java pattern but storing generic things
Is it possible to create a typesafe ...
2
votes
2
answers
1k
views
Static Factory Methods - return any subtype
I'm reading the book Effective Java and Joshua Bloch, in chapter 2, said:
A third advantage of static factory methods is that, unlike constructors, they can return an object of any subtype of their ...
5
votes
0
answers
1k
views
Java reserved type name "var" best practices? [closed]
Unfortunately, there is no item in Effective Java that discusses the reserved type name "var"(since it is introduced in java 10).
There is a saying to use interfaces as types whenever you can (Item ...
2
votes
2
answers
623
views
What are the drawbacks in returning empty immutable collection instead of allocating a new and empty one?
I used to call for Collections.emptyList(), emptySet and emptyMap to return a reference to an immutable empty collection instead of null from my functions. That's because I see no reasons to allocate ...
1
vote
1
answer
187
views
Why is sharing the mutable internals of immutable objects a good idea?
I'm reading Joshua Bloch's "Effective Java" and have two questions. Bloch states the following.
Not only can you share immutable objects, but they can share their internals.
He then proceeds to ...
1
vote
1
answer
90
views
Why is the boolean logical operator ^ being used in this piece of example code from Effective Java?
I found this example code for Joshua Bloch's book, Effective Java. It's meant to demonstrate why you should avoid unnecessarily creating objects:
import java.util.regex.Pattern;
// Reusing expensive ...
2
votes
3
answers
141
views
How is the JVM prevented from 'optimizing away everything' in this piece of example code from Effective Java?
I found this example code for Joshua Bloch's book, Effective Java. It's meant to demonstrate why you should avoid unnecessarily creating objects:
public class Sum {
private static long sum() {
...
13
votes
4
answers
3k
views
How does casting this object to a generic type work?
My understanding is generic types are invariant, so if we have B as a subtype of A, then List<B> has no relationship with List<A>. So casting won't work on List<A> and List<B>.
...
5
votes
2
answers
768
views
Effective Java item 16 (2nd edition) - Is Forwarding class only used to allow re-use?
I am going through Effective Java, Item-16 Favor composition over inheritance. I looked at the Forwarding class example below.
I am wondering what's the point of having a ForwardingSet class? ...
1
vote
3
answers
407
views
Make my logger very effective to my Java-application
I am struggling with the following problem and ask for help.
My application has a logger module. This takes the trace level and the message (as string).
Often should be messages constructed from ...
0
votes
3
answers
481
views
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 ...
6
votes
3
answers
585
views
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 ...
5
votes
1
answer
3k
views
Why javadoc parameter-less constructors?
In item 56 of Effective Java (Third Edition), Joshua Bloch states: "Public classes should not use default constructors because there is no way to provide doc comments for them."
The default ...
9
votes
1
answer
1k
views
In constructor method references, difference between using generic type parameters and not?
I'm reading Effective Java 3 and noticed this code in Item 43: “Prefer method references to lambdas”:
TreeMap<K,V>::new
Notice the type parameters. I've always just done:
TreeMap::new
I use ...
1
vote
2
answers
2k
views
BigDecimal is extendable and has no copy-constructor. Is that a security risk?
According to Effective Java Item 24 (Make defensive copies when needed) mutable objects pose a security risk, especially when passed as constructor arguments. One is encouraged to make defensive ...
5
votes
1
answer
589
views
What is wrong with this Java Puzzlers piece of code?
In new, third edition of Effective Java Joshua Bloch mentions piece of code from Java Puzzlers (it's about closing resources in try-finally):
For starters, I got it wrong on page 88 of Java ...
1
vote
1
answer
2k
views
What is the better way to implement CountDownLatch in effective java 2nd item 72?
Effective Java Item 72 shows bad example of CountDownLatch implementation. But it doesn't show a right way to implement it. Do I have to use wait() and notify() instead of a while loop ?
Can anyone ...
1
vote
2
answers
654
views
Singleton classes serializable [duplicate]
Can anyone explain this paragraph: [copied from Effective Java Joshua Bloch 3rd edition Chapter 2 Item 3 ]
To make a singleton class that uses either of these approaches (i.e. ...
2
votes
1
answer
318
views
What is the best way of sending defensive copy of data ?
I just have read effective java rule 39 (Defensive Copy). It is not told explicitly, two times of copy should occur every data transaction to follow this rule. Below is the example code I thought. It ...
1
vote
2
answers
770
views
Immutable class using static factories
I'm studying Effective Java by Joshua Bloch, where he explains about different ways of achieving immutable class. To prevent subclassing, one way is to use final. The more sophisticated way for it is ...
-2
votes
1
answer
45
views
about inline the call from effective_java book
i have a question in effective_java this book,
what's mean of the 'modern Java virtual machine (JVM) implementations are almost certain to inline the call to the static factory method.'
i don't ...
1
vote
1
answer
232
views
Java: why class designed for inheritance should contain protected clone method?
I'm reading carefully Effective Java (by Joshua Bloch) and I found the following sentence on cloning:
If you design a class for inheritance, be aware that if you choose not to provide a well-...
3
votes
2
answers
2k
views
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 ...
0
votes
2
answers
815
views
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:
// ...
2
votes
2
answers
1k
views
Returning empty collections in accordance with Effective Java
Effective Java, Item 43 states:
return unmodifiable empty collections instead of null.
So far so good. Are there any guidelines, exactly what to return? Does this question even make sense? What ...
-1
votes
2
answers
219
views
Effective Java: clone() private access [duplicate]
In Bloch's Effective Java, 2nd edition, Item 11: Override clone judiciously has the following example:
class Stack {
private Object[] elements;
private int size = 0;
private static final ...
2
votes
2
answers
757
views
Effective Java Item 66: Why to synchronize both read and write methods?
In Effective Java -> Item 66, Joshua stresses on the need to synchronize both read and write operations to avoid liveness failure.
Here in this particular example, I think synchronization on write ...
4
votes
2
answers
129
views
Is terminating an object is the same as nulling it?
So i have been going through "Effective Java 2nd Ed."
In item 7 he talks about not using finalizers because they can cause a lot of problems .
But instead of using finalizers we can " provide an ...
1
vote
2
answers
1k
views
Examples for combinatorial explosion in Java?
In the Effective Java, Item - 18, Bloch says that interfaces prevent combinatorial explosion, which will happen when using abstract classes with multiple attributes in a type system.
I am not able to ...
0
votes
2
answers
555
views
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 ...