How many access levels does Java have anyway?

Before I start with my new post, I would like to talk a little about my previous one: “Rule for public void method signatures“. As it turns out, this is a proposal for Java 7 called “chained invocations”. You can find more details here. There is also another way of doing this called “Double Brace Initialization“:

JPanel panel = new JPanel() {{
  setLayout(new BorderLayout());
  setBorder(BorderFactory.createEmptyBorder());
}};

At first site, it seems useful in a number of situations (i.e. Graphical User Interfaces), on the other hand, many suggest that this should not be abused as it creates ugly and cluttered code.

Now, let’s put that in the side and go back to what you have learned in Java programming.

Continue reading…

Posted by Panagiotis Peikidis on 27 Nov 2008 - 1 Comment »

Idea: Rule for “public void” method signatures

I don’t remember in which book I read this, but the book created a wrapper class for MenuItem that when calling a setter it would return the class itself. So I started thinking: Would it be a bad practice if every time I created a class I would replace a public void with a public MyClass? In other words, use this as a general rule. Why don’t we see?

Continue reading…

Posted by Panagiotis Peikidis on 09 Apr 2008 - 1 Comment »

Dive In: JSnake

Continuing on the Dive In series, we are going one step further and creating a small game using animation: a clone of the classic video game Snake. As always, this will be as small as possible and many many changes can be made (which I’ll once again try to point out). We’ll start from design all the way to implementing and testing it using the easy Water Fall model. You can run the game and download the source code at my JavaCorner.

So, without further ado, let’s start!

Continue reading…

Posted by Panagiotis Peikidis on 25 Mar 2008 - No Comments »

Playing with Try, Catch and Finally

I read a blog post (Greek) recently that described the path Java takes when executing a try, catch, finally block. But before I continue I would like you to consider the following code. What will be printed out?

public class ErrorClass {
    public static void main(String[] args) {
        System.out.println(getMessage());
    }
 
    private static String getMessage() {
        String toReturn = "Initializing";
        try {
            throwException();
            toReturn += "+No Exception";
 
            return toReturn;
        } catch (Exception e) {
            toReturn += "+Catch";
 
            return toReturn +" from Catch";
        } finally {
            toReturn += "+Finally";
 
            return toReturn +" from Finally";
        }
    }
 
    private static String throwException() throws Exception {
        throw new Exception("original exception");
    }
}

a) “Initializing+No Exception”
b) “Initializing+Catch from Catch”
c) “Initializing+Catch+Finally from Catch”
d) “Initializing+Catch+Finally from Finally”

Continue reading…

Posted by Panagiotis Peikidis on 16 Mar 2008 - No Comments »

Dive In: JSameGame

In this post I’ll create a small game project in Java from scratch based on the game SameGame. There are numerous of ways to create this game. I’ll try to make it as simple as possible and use Good Practices as much as I can. We’ll go step by step all the way from designing the idea to creating and testing it. Because this is a small project, we will be using the Waterfall Model. This post is dedicated to a friend of mine, Niko (see, it’s that simple! ;) ). The game along with the source code can be found at pek’s Java Corner.

So, let’s begin!

Continue reading…

Posted by Panagiotis Peikidis on 24 Feb 2008 - 1 Comment »