What static methods for interfaces tell you about the Java community

Recently I was putting together some code for an OSGi plugin at work. I created an interface to add listeners to an extension point. The code looked something like this:

The idea was simple: At some points in my application I would call all registered listeners, pass them an object and let them do whatever they are supposed to do. The only problem was, that the code to call these listeners is pretty cumbersome:

FML! In reality it looks even more cumbersome with some exception-handling and logging.

So the first thing I did was a utility class to put that code to and never see it again. But utility classes are not a very good practice at all. So I thought about a suitable place for my code. Some kind of a factory? Or some invoker object? But isn’t that just another name for a utility class? Or… why not put it in the interface itself? So the method to deal with the interface would stay in the interface itself.

A minute and a compile time error later it came back to my mind: interface can’t have static methods in Java! (I will come back to this later…)

But why can’t interfaces have static methods?

The question seems legit to me. Classes as well as abstract classes can have static methods and interfaces can have static classes and static fields. But no static methods. Why?

Ask Google!

I did what I’m always doing when I have a question, I asked Google. Five minutes later I had a bunch of different answers:

It’s not possible!

Everything is possible. Especially in Computer Science.

It doesn’t make sense!

I can give you 10 situations in a minute where it makes sense. Let’s start with the one above.

Your desing is wrong!

Is it? Would a new class with just a single method make it better?

You know nothing about OO!

So if I put a static method in an abstract class it’s called a pattern (the Factory Pattern of the GoF) and if I want to do something similar in an interface, I have no clue what I’m doing? Languages like Ruby which are OO from the ground up (not like Java) have similar concepts.

The truth?

As always in software development there’s no certainty. There are a thousand ways to implement the case a both, every with its own pros and cons. One is beautiful OO, one reads nice, another one is easy to test and one is just as sophisticated as it could be. But the best explanation I found is this:

It’s just a design decision of the Java makers.

It is! Like everything that is in the language or not. And as many things that were discussed over the years, it will finally come with the next Java release. Java 8 will have static methods for interfaces. It will also have default implementations for interfaces and Lambda-expressions. OO? Nope, just good to write code.

The community

I looked in a dozen different forums and Q&A sites for a proper explanation. But I didn’t find much good answers. It is just a decision of some guys made a couple of years ago. And right now, it seems that they are doing the very reverse – put static methods for interface in Java 8.

But what really shock me is the way it is discussed in the community. A lot of people are almost religious about it. They don’t have a clue what it’s about, so it must be huge design mistake, not OO or even not possible.

I think strong opinions are never. Not in politics, not in art and especially not in software development. Maybe that’s why Java moves as fast as it does?

The hack

Btw, there is a very easy hack to add static methods to an interface in Java 7. Interfaces can have static classes which can have static methods. It’s not the nicest thing ever, but at least it lets you put your “utility class” together with the interface.

Best regards,
Thomas

Trip to Croatia

My girlfriend and I used the last sunny days in this summer for a trip to Croatia. Being the fist time in Croatia, we were very impressed by its nature, its people and beautiful places. Here are some of our impressions, including Ljubljana, the capital of Slovenia.

Best regards,
Thomas

First steps with Ractive.js

During the summer I had a little bit of free time. I had no university courses, almost no on-going projects and was only working for two days a week. So I decided to build a small web app.

Since I was couriosly waiting for some grades to arrive in the online portal of my university, I decided to build a (mobile) app to check this.

I decided to build the app with Python on the server and some sort of JavaScript framework on the client. Many of my fellow students were talking about AngularJS, but I wasn’t sure if I should take such a big framework for my first steps (I never used a framework like this in JavaScript before). Then I stumbeled over this link on HN. It was about an Angluar-like framework called Ractive.js.

Ractive.js

Ractive.js provides two main features (in my opinion):

  1. It provides a two-way data-binding. This means that you can hand-in a JSON object and directly bind its attributes to HTML elements. So you don’t have to update certain HTML elements manually (e.g. via JQuery).
  2. It provides a template rendering. This means you can write HTML templates (as simple *.html files for example) with mustach-syntax and render them on the client via JavaScript. To develop single-page applications gets very easy with this.

The server part

First of all, I did the server part. I used a combination of twill and Beautiful Soup to parse the web portal of my university. This was (and still is) a little bit slow, but at least no big deal. Then I build a small API to access these via AJAX. I used this API later on to update my page. E.g. the call http://127.0.0.1:5000/grades would return a JSON containing all of your grads:

I have build similar APIs for your personal schedule, for our canteen and some more.

The client part

For the client part, I used Pure (CSS framework) and (of course) Ractive.js. This made my templates really short and gave me a good experience on both, classical browsers and mobil. Here’s the full template to render the grade-table:

I simply loaded the template (which is actually a *.html file) in my main-template. Afterwards I have it in a variable called gradesTemplate in this example:

Then I am able to render it, e.g. by adding an appropriate click-listener to a link. The listener will fetch some data via AJAX and render the templated (loaded before) with it:

I’m not completly happy with using JQuery, but to put it all together was quite easy.

The result

Here are some screenshot of my final version (it’s not running online and you would need an account of my university to use it, so some screenshot are the best I could provide).

The code

You can find the code on BitBucket:

Best regards,
Thomas Uhrig

When your Acer Aspire 5560G doesn’t shutdown anymore

Last year I bought an Acer Aspire 5560G as my second laptop (yes, sometimes two are better than one). A half year later, I was working pretty much with this laptop and decided to upgrade it with a SSD drive. So I bought a brand new Samsung SSD 840 PRO with 256 GB. I was very happy. Well, was…

After installing the SSD and reinstalling Windows 7 there was something strange – Windows didn’t shutdown anymore. Whenever I tried to shutdown my laptop (or restart it or put it to hibernate) Windows went off, but the laptop was still on. The ventilator was running, power was on and the lights at the front of my laptop were blinking. So what?

So I tried to fix it. I reinstalled Windows (both, Windows 7 and Windows 8.1). I installed all kind of driver, even of devices I don’t have. I updated, I formated, I read the internet and so on. I even flashed my BIOS to be sure. But nothing worked for me.

Last night I was searching the web again and I was very despaired. I found a forum post of some guy who had the same problem. And his solution was pretty simple: he installed Windows using an USB driver rather than a DVD. I thought what the fuck, he’s joking, forget about it. But as I woke up this morning I was still despraid and I thought, well, let’s try it. I mean, why not?

So I put my Windows 7 DVD into my drive and copied it to an USB drive using some dubios freeware software that tried to install one browser-toolbar after another. But ten minutes later I had my Windows copy on my bootable USB stick. Restart, format, install, waiting… During the installation the laptop restarted several times and I assumed that this is a good sign.

After aproximately 30 minutes the big moment came and I pressed on “shutdown” in my brand new Windows 7 installation. Aaaannnddd baaammm it went off!

I studied 11 semesters of computer science right now. I installed all kinds of Windows (98, XP, Vista, 7, 8), some Linux distributions and other stuff. But that there is a difference between installing Windows from a DVD or from an USB stick doesn’t make any f***ing sense to me. Thank you Microsoft.

reinstalling_windows

Best regards,
Thomas Uhrig