Runcible Blog

Another Reason Why Java Sucks

I found another seemingly trivial task that Java forces the programmer to jump through hoops to accomplish: copying an object. You can't get more basic than that, or so I thought.

Let's say there's a Subscriber class which is a very large bean that holds everything there is to know about a subscriber. In the servlet I was modifying, I wanted to load a Subscriber from the database, make a copy of that subscriber, and change one field, the publication that the subscriber subscribes to. I had to keep the original subscriber, too.

I knew that Object has a clone() method because of my supremely bloated IDE which pops up a list of methods for everything. I figured it would be a simple (for Java anyway) matter of saying:

// get the publication
String pubType = request.getParameter("pubType");
// Static method to load the requested Subscriber.
Subscriber oldSub = Subscriber.loadSubscriber(params go here);
// copy the old one to a new Subscriber (notice the annoying casting involved)
Subscriber newSub = (Subscriber) oldSub.clone();
// set new subscriber's publication.

But that's only part of it. The Subscriber class also needs to implement the Cloneable interface and apparently, override the clone() method like so:

public class Subscriber implements Cloneable {
    // a lot of bean code...
    public Object clone() {
        Object obj = null;
        try {
            obj = super.clone();
        } catch (CloneNotSupportedException ex) {
        return obj;

(that's another thing about our code; we never do anything with caught exceptions. What's the point of having exceptions if you never even print out the result?)

All I wanted to do was copy an object! Java is so anal about protecting the programmer from himself that the verbosity and required hoop-jumping get in the way most of the time. Why can't there be an easy way to copy an object, and if you need finer control, then use the more complicated method?

Well, Python comes to the rescue again! Here's how you might do it in Python.

import copy

pubType = "Stinky Times"
# You probably wouldn't do something like a JavaBean
# in Python.  Nor would the bean know how to "load itself".
# For the sake of the example, assume that
# Subscriber's constructor takes the required arguments
# to load itself.
oldSub = Subscriber(params go here)

newSub = copy.copy(oldSub)


The "Python Way" assumes that you want to do something simple most of the time, so the language is very easy to do most things. Of course, if you want to do fancy things like control how your object is copied or make a "deep copy", you can do that too (override __copy__() in your class and use copy.deepcopy(), respectively).

But the point is that Python doesn't force you to do it the hard way for simple things. In that sense, it's more "scalable" than Java. You can use the basic features for small tasks and gradually use more and more of the language for more complicated projects. You don't have to know about everything all at once.

My boss was defensive when I remarked that the Java way was convoluted and unnecessary. He said, "You have to do it that way because it's the Object-Oriented Paradigm." and trailed off into some lame excuse. The problem isn't object-orientedness -- it's that Java sucks.