h1

Inner classes.

April 17, 2010

Why are they usefull?

I would say that I would use them just to let the next programmer know that I only intend this classes to be used in this particular class and in no other place. Just like that.

I was working on some project and we had to deal with using the RaphaelGWT port of RaphaelJS.

We stumbled upon they were a lot of inner classes. Have a look at this snippet of the code:

package com.hydro4ge.raphaelgwt.client;

import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.ui.Widget;
import java.util.ArrayList;

public class Raphael extends Widget {

    private RaphaelJS overlay;
    private final ArrayList shapes = new ArrayList();

    public Raphael(int width, int height) {
        super();
        Element raphaelDiv = DOM.createDiv();
        setElement(raphaelDiv);
        overlay = RaphaelJS.create(raphaelDiv, width, height);
    }

    public class Shape extends Widget {
    }

    public class Circle extends Shape {
    }

    public class Text extends Shape {
    }

    public class Rect extends Shape {
    }

    public class Ellipse extends Shape {
    }

    public class Image extends Shape {
    }

    public class Path extends Shape {
    }
}

That code was taken from RaphaelGWT repository.

The thing is that those classes are used within that same class, over and over again, and nowhere else. So basically, from this particular class, I can say that if we have to use some classes that are really part of the containing class and will never ever be used outside of it, we should just create it as an inner class.

I still have to check (actually i should just google for it and that’s it) what the scope of things are for those inner classes and the containing class… I mean, can the inner class methods view the private attributes of the container class?
Why? Why not?

How about inheritance and all that stuff?

Well, surely it’s a long subject.

h1

Hello world and some interesting stuff

December 11, 2008

public class Hello {
  public static void main(String args[]) {
    System.out.println("Hello world!")
  }
}

Some interesting things in this simple example

Everything has to be in a class

The first thing I noticed is that everything has to be in a class, even for something as easy and straighforward as the “Hello world!” mandatory example.

Actually not everything has to be in a class (yeah, I’m a contradicting man). Enumerations are, I think, the only thing that dont, however, if you look at them reaaaally closely, you’ll see that they are, more or less, like classes. I suppose I’ll talk/write about this in the near future

There has to be a main method

If you want to execute the example, be sure you have a main method, and that it accepts an array of arguments. This might sound pretty stupid for people that already know that language, but in Python for example, you don’t have this constraint.

There are some packages that are always at hand

There is no need to import the System package. I think that there are some other packages that are always at hand.

So many things in just a couple of lines…

cya!

Follow

Get every new post delivered to your Inbox.