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
0
votes
1 answer

Static Factory Methods getType & newType examples

In Effective Java , Item 1, towards the end of the this section on Static Factory methods, Joshua Bloch describes common names for static factory methods. Two of these names, getType and newType, are a little confusing for me. Quote [Bloch, p.…
FlatPenguin
  • 121
  • 1
  • 13
0
votes
2 answers

How to create an abstract class with static factory method?

I have a "standard" JPanel with two panels inside. I was trying to create a kind of template class and then extend it and implement the content. The question is about which would be the way to implement it. The code below is what I'm trying to make…
0
votes
1 answer

Are raw types always bad?

Item 23 ( Don't use raw types in new code ) of Effective Java claims that using raw types in java code is always dangerous For instance, it claims that the following method is dangerous and unsafe // Use of raw type for unknown element type - don't…
Karthick
  • 2,844
  • 4
  • 34
  • 55
0
votes
2 answers

Is it possible to generate builder code automatically in Eclipse according to builder pattern?

There's builder pattern which was introduced by Joshua Bloch in him book Effective Java (2nd Edition), you can see description and examples of this pattern here: Too Many Parameters in Java Methods, Part 3: Builder Pattern Is there any way to make…
Anatoly
  • 5,056
  • 9
  • 62
  • 136
0
votes
2 answers

What is the use of removeRange method of AbstractList class in subclasses

In Effective Java, it is stated that removeRange method is of no interest to end users of a List implementation. It is provided solely to make it easy for subclasses to provide a fast clear method on sublists. In the absence of the removeRange…
user961690
  • 698
  • 1
  • 11
  • 22
0
votes
1 answer

what is LINE_WIDTH in the code below?

This code from effective java in Item 51: Beware the performance of string concatenation public String statement() { StringBuilder b = new StringBuilder(numItems() * LINE_WIDTH); for (int i = 0; i < numItems();…
das kinder
  • 1,180
  • 2
  • 10
  • 16
0
votes
8 answers

How much should a function trust another function

Consider the following two functions in a class called Graph. The entire source code is found here: http://www.keithschwarz.com/interesting/code/?dir=dijkstra. public void addNode(T node) { mGraph.put(node, new HashMap()); …
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
3 answers

How should private functions do parameter validation or user input and internal data structure?

The following code, would return pairs of integers which sum to x, eg: if arr {1, 2, 3, 4, 5] and x is 7 then list should contain {3, 4} and {2, 5}. Main goal is to understand how parameter validation should be performed in private method. Question…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
1 answer

Should exception check be explicitly done in public function if private function needs the same input

The effective java makes it clear that an assert be used to verify parameters of a private function. If a method is a public method then the method should throw a NPE if a null is an invalid parameter. Eg: public void foo (Sting str) { char[] ch…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
3 answers

Effective Java Item 6 - Stack pop implementation

I've been reading Effective Java, and one thing that came out to me with the obsolete object reference item was his implementation of pop(): public Object pop(){ if (size == 0) throw new EmptyStackException(); Object result =…
Jason
  • 11,263
  • 21
  • 87
  • 181
0
votes
2 answers

Which interface to prefer and on what basis?

Effective java encourages use to interfaces rather than abstract / concrete classes. Question is if there is an interface heirachy, which interface type should be chosen and why ? For example, an ArrayList implements List which implements Collection…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
2 answers

To null check or not to do a null check?

Here is a code authored by Josh bloch, ( Linkedlist.java) * @throws NullPointerException if the specified collection is null */ public boolean addAll(int index, Collection c) { checkPositionIndex(index); …
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
3 answers

Instance variable verification

Reading effective java, it mentions that we need to validate method parameters as a good practice, throw exceptions for public and assert for private methods. But, do we need to take any measure against instance variable. EG: (dont take example as a…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
3 answers

Should interfaces be used for type arguments

I have read Effective Java which emphases on using Interfaces where possible as return types. Extending the argument I was wondering which of the following methods was preferred / considered a good practice. OPTION 1: Map> map…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
0
votes
2 answers

Dependency object be saved as instance variable or just the values obtained from getters?

Consider an example of a simple 'MyCalendar' class with 3 getters 'getDay, getMonth, and getYear'. If I pass 'MyCalendar' object to my another class which of the following options would be a good approach. OPTION 1: Call required parameters through…
JavaDeveloper
  • 5,320
  • 16
  • 79
  • 132
1 2 3
11
12