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
90
votes
5 answers

What is an AssertionError? In which case should I throw it from my own code?

In Item 2 of the "Effective Java, 2nd edition" book, there is this snippet of code, in which the author wants to forbid the empty initialization of an object. class Example { private Example() { throw new AssertionError(); } } The…
doplumi
  • 2,938
  • 4
  • 29
  • 45
41
votes
8 answers

Why are public static final array a security hole?

Effective java says: // Potential security hole! static public final Thing[] VALUES = { ... }; Can somebody tell me what is the security hole?
unj2
  • 52,135
  • 87
  • 247
  • 375
39
votes
6 answers

Why are readObject and writeObject private, and why would I write transient variables explicitly?

I am reading the chapter on Serialization in Effective Java. Who calls the readObject() and writeObject()? Why are these methods declared private ? The below is a piece of code from the book // StringList with a reasonable custom serialized…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
35
votes
2 answers

Understanding the concept behind Service provider framework like JDBC using the factory method

From Effective Java (Item 1: Consider static factory methods instead of constructors): The class of the object returned by a static factory method need not even exist at the time the class containing the method is written. Such flexible static…
Geek
  • 26,489
  • 43
  • 149
  • 227
34
votes
3 answers

What does "Recursive type bound" in Generics mean?

I am reading the chapter on Generics from Effective Java[Item 27]. There is this paragraph in the book: It is permissible, though relatively rare, for a type parameter to be bounded by some expression involving that type parameter itself. This is…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
25
votes
8 answers

What's the difference between raw types, unbounded wild cards and using Object in generics

I am reading the chapter on Generics in Effective Java. Help me understand difference between Set, Set and Set? The following paragraph is taken from the book. As a quick review, Set is a parameterized type representing a set …
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
25
votes
6 answers

Effective Java By Joshua Bloch: Item1 - Static Factory Method

I am reading the Effective Java by Joshua Bloch and I have question about Item1 Static Factory Method. Quote[Bloch, p.7] Interfaces cant have static methods, so by convention, static factory methods for an interface named Type are put in…
Thang Pham
  • 38,125
  • 75
  • 201
  • 285
23
votes
4 answers

Java: When to add readObjectNoData() during serialization?

I am reading the serialization chapter in Effective Java. I am trying to understand the paragraph below, which is found in the book. If you implement a class with instance fields that is serializable and extendable,there is a caution you should be…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
19
votes
3 answers

How does the JVM reuse interned String substrings?

I'm aware if you make for (condition) { String s = "hi there"; } Just one String instance is created in all the iterations, unlike String s = new String("hi there"); that will create a new instance in each iteration. But, reading Effective Java…
Jordi Castilla
  • 26,609
  • 8
  • 70
  • 109
18
votes
4 answers

What is a nonmemory resource?

I am reading "Effective Java". In the discussion about finalize, he says C++ destructors are also used to reclaim other nonmemory resources. In Java, the try finally block is generally used for this purpose. What are nonmemory…
Vinoth Kumar C M
  • 10,378
  • 28
  • 89
  • 130
18
votes
3 answers

Class.asSubclass signature

my question is quite theoretic... This is the signature of Class.asSubclass (Javadoc): public Class asSubclass(Class clazz) Why are wildcard generics used in the return type? From my understanding of generics a better signature…
CptWasp
  • 459
  • 2
  • 13
17
votes
2 answers

Effective Java Item 47: Know and use your libraries - Flawed random integer method example

In the example Josh gives of the flawed random method that generates a positive random number with a given upper bound n, I don't understand the two of the flaws he states. The method from the book is: private static final Random rnd = new…
17
votes
3 answers

Why do we need bounded wilcard in Collections.max() method

I've read awesome "Effective Java" by Joshua Bloch. But one example in the books is left unclear to me. It's taken from chapter about generics, exact item is "Item 28: Use bounded wildcards to increase API flexibility". In this item it's shown how…
east825
  • 909
  • 1
  • 8
  • 20
14
votes
2 answers

What does it mean to say that int enum patterns are compile-time constants?

This is from Effective Java Programs that use the int enum pattern are brittle. Because int enums are compile-time constants, they are compiled into the clients that use them. Can some one explain why the int enum pattern is called compiled…
Geek
  • 26,489
  • 43
  • 149
  • 227
13
votes
4 answers

Do we ever need to prefer constructors over static factory methods? If so, when?

I have been reading Effective Java by Joshua Bloch and so far it really lives up to its reputation. The very first item makes a convincing case for static factory methods over constructors. So much that I began to question the validity of the good…
1
2 3
11 12