Sunday, 6 July 2014

Source code issues

Well apparently the gist loader I've been using for displaying gists in here is broken. I didn't have time to investigate, but I think I'm going to switch to Alex Gorbatchev's Syntax Highlighter.

I didn't manage to get it working yet. I'll keep trying it on this page.

public class A {
    public A() {
        System.out.println("Hello, syntax highlighter!");
    }
}

Saturday, 5 July 2014

Java constructors

I get this error from time to time and you may get it too:

"There is no default constructor available in com.example.A"

Here's a sample code / pattern (error is shown by my IDE on line 7):

public class A {
    public A(int a) {

    }
}

class B extends A {
}

The reason for that error is very clear if you read the Java language specification, the section(s) on constructors.

In Java, every class has a constructor. If you don't declare you own, a default (no-argument) constructor will be created implicitly for you. If you do declare your own constructor, then no no-argument constructor will be created implicitly - you'll have to create it manually if you want one. The default constructor has the access modifier of the class (so, for example, if your class is "public", then the implicitly created default constructor will also have the "public" access modifier etc.).

So both classes in the above code are guaranteed to have a constructor: A has an explicit one and B has an implicit no-argument constructor. Here's the essential thing that you are required to know to understand why the error takes place: in Java, all constructors start with a call to a superclass constructor. The exception is the Object class, which has an empty constructor. If you explicitly call a superclass constructor (using super([parameters])), then this requirement is fulfilled. If you don't, then the default constructor of the superclass is called implicitly.

However, if your superclass doesn't have a default constructor, like in A's case above, this will result in a compile-time error. You will therefore need to call a superclass constructor explicitly in B, which in turn requires you to create a constructor explicitly in B.

Yet another situation when knowing the language specification comes in handy. :)

Learning Java

What's the best resource for learning Java? I assume the one asking this question has some experience with the language. I probably asked this question several times since I first wrote my first line of Java a while ago.

Sure, "learning Java" is a bit broad. You can "learn", for example, how to add a tool tip to a JButton. StackOverflow is obviously the top choice for these very specific things. Then the Oracle tutorials are also good at introducing numerous topics and the API. But I am talking more about the language itself than its APIs. 

There has been one resource in particular that greatly improved my understanding and knowledge of the language in a way that nothing else came even close: THE. JAVA. LANGUAGE. SPECIFICATION. (along with the JVM specification)

There are some things about the language that you may not learn at university or from a random tutorial / article online, but all will be covered in the language specification. It's a document I constantly read to refresh my memory and understand things better and I think all Java developers should have a copy handy.