Skip to content

Say "Hello World!" Without Main Method in Java

Can you print out “Hello World!” without writing a main method in Java? Think for a while. Yes, you’re right. It can be possible using “static initialization blocks”. Let’s see the source code first:
package net.cavdar.staticinitializer;

* Says “Hello World!” without main method. A simple use of
* static initialisation.
* @author accavdar
public class HelloWorldWithoutMain {
static {
System.out.println(“Hello World!”);
System.exit(0); // prevents “main method not found” error

So, what is “static initializer block”?

  • A static initializer block is defined using the keyword static.
  • The code in a static initializer block is executed once by the virtual machine when the class is loaded.
  • A static initializer block cannot contain a return statement. Therefore, no need to specify a return type.
  • A static initializer block doesn’t have an argument list.
  • It can initialize only static data members of the class.

Because the static initializer block is executed when the class is first loaded, we can print out “Hello World” without writing a main method. The execution is stopped using “System.exit()” command. So, we prevent “main method not found” error. It is tricky. Isn’t it?

Happy coding. :D

Note: Write fully qualified class name (for our example “net.cavdar.staticinitializer.HelloWorldWithoutMain”) for configuring run options for main class, if you need.

Posted in Java, Tips & Tricks.

Tagged with , , , .

JUnit 4 in 60 Seconds

I played with JUnit 4 library this weekend and here is the short introduction to it:

  1. @Test
    Mark your test cases with @Test annotations. You don’t need to prefix your test cases with “test”.  In addition, your class does not need to extend from “TestCase” class.
    public void addition() {
    assertEquals(12, simpleMath.add(7, 5));

    public void subtraction() {
    assertEquals(9, simpleMath.substract(12, 3));

  2. @Before and @After
    Use @Before and @After annotations for “setup” and “tearDown” methods respectively. They run before and after every test case.
    public void runBeforeEveryTest() {
    simpleMath = new SimpleMath();

    public void runAfterEveryTest() {
    simpleMath = null;

  3. @BeforeClass and @AfterClass
    Use @BeforeClass and @AfterClass annotations for class wide “setup” and “tearDown” respectively. Think them as one time setup and tearDown. They run for one time before and after all test cases.
    public static void runBeforeClass() {
    // run for one time before all test cases

    public static void runAfterClass() {
    // run for one time after all test cases

  4. Exception Handling
    Use “expected” paramater with @Test annotation for test cases that expect exception. Write the class name of the exception that will be thrown.
    @Test(expected = ArithmeticException.class)
    public void divisionWithException() {
    // divide by zero
    simpleMath.divide(1, 0);
  5. @Ignore
    Put @Ignore annotation for test cases you want to ignore. You can add a string parameter that defines the reason of ignorance if you want.
    @Ignore(“Not Ready to Run”)
    public void multiplication() {
    assertEquals(15, simpleMath.multiply(3, 5));
  6. Timeout
    Define a timeout period in miliseconds with “timeout” parameter. The test fails when the timeout period exceeds.
    @Test(timeout = 1000)
    public void infinity() {
    while (true)
  7. New Assertions
    Compare arrays with new assertion methods. Two arrays are equal if they have the same length and each element is equal to the corresponding element in the other array; otherwise, they’re not.

    public static void assertEquals(Object[] expected, Object[] actual);
    public static void assertEquals(String message, Object[] expected, Object[] actual);

    public void listEquality() {
    List expected = new ArrayList();

    List actual = new ArrayList();

    assertEquals(expected, actual);

  8. JUnit4Adapter
    Run your Junit 4 tests in Junit 3 test runners with Junit4Adapter.
    public static junit.framework.Test suite() {
    return new JUnit4TestAdapter(SimpleMathTest.class);

Happy coding. :D

Posted in TDD.

Tagged with , , .

Born of a Cuddly Penguin: TUX, Linux Mascot

Have you ever wondered how TUX borned? Yes, I have. Yesterday night, I performed a little search on net and here is the result:

Everthing starts with a debate on the linux-kernel mailing list about a suitable logo/mascot for Linux. Many many different ideas including noble beasts such as Sharks or Eagles and inspirations from other operating system logos suggested before Linus Torvalds (the father of Linux) said: “I am rather fond of Penguins”. :D

After several attempts to draw Penguins in various poses, someone suggested a Penguin holding up the world. Here is the part of famous email from Linus as a response:

So when you think “penguin”, you should be imagining a slighly overweight penguin (*), sitting down after having gorged itself, and having just burped. It’s sitting there with a beatific smile – the world is a good place to be when you have just eaten a few gallons of raw fish and you can feel another “burp” coming.

(*) Not FAT, but you should be able to see that it’s sitting down because it’s really too stuffed to stand up. Think “bean bag” here.

Now, if you have problems associating yourself with something that gets off by eating raw fish, think “chocolate” or something, but you get the idea.

Ok, so we should be thinking of a lovable, cuddly, stuffed penguin sitting down after having gorged itself on herring. Still with me?

OK, but why penguins? Linus explains his love of Penguins in another e-mail as follows:

“Linus likes penguins”. That’s it. There was even a headline on it in some Linux Journal some time ago (I was bitten by a Killer Penguin in Australia – I’m not kidding). Penguins are fun.

As to why use a penguin as a logo? No good reason, really. But a logo doesn’t really ave to _mean_ anything – it’s the association that counts. And I can think of many worse things than have linux being associated with penguins.

Having a penguin as a logo also gives more freedom to people wanting to use linux-related material: instead of being firmly fixed with a specific logo (the triangle, or just “Linux 2.0″ or some other abstract thing), using something like a penguin gives people the chance to make modifications that are still recognizable.

So you can have a real live penguin on a CD cover, for example, and people will get the association. Or you can have a penguin that does something specific (a Penguin writing on wordperfect for the WP Linux CD, whatever – you get the idea).

Compare that to a more abstract logo (like the windows logo – it’s not a bad logo in itself). You can’t really do anything with a logo like that. It just “is”.


Where does the name TUX come from? The first person to call the penguin TUX was James Hughes, who said that it stood for Torvalds UniX in a thread called “Let’s name the penguin” and it is accepted. However, many people observe that TUX is also an abbreviation of tuxedo, the outfit which springs to mind when they see a penguin.

And lastly, you can find lots of funny TUX pics from here. Enjoy it…

Posted in Linux.

Tagged with , , , .

3 Ways of JDK Source Code Attachment in Eclipse

You wanna look at a JVM class while you are coding and you cannot. Here is the solution.

First of all, download your related JDK source code files unless you already have it. In general, source code is included in the JavaSE bundle and located under the root directory of your installation ( mostly).

1. Try to “Open Decleration (F3)” to any JVM class (i.e String class). You will take “Source Not Found” message and below it you will see Attach Source… button. Press it, select the source code file (External File…) and press OK.

Method 1

2. Go to Project > Properties > Java Build Path > Libraries and expand JRE System Library [your jre version] then, rt.jar. Select Source attachment, click Edit…. Select the source code file (External File…) and press OK.

Method 2

3. Go to Window > Preferences > Java > Installed JRES and click Edit… for your desired JRE. Expand rt.jar, select Source attachment and click Source Attachment…. Select the source code file (External File…) and press OK.

Method 3

You can use the above techniques for other libraries that you need to navigate/discover if their source codes are in your hand.

And you’re OK now. Happy coding. :D

P.S: Menu structure is taken from Eclipse 3.4.0 version.

Posted in Tips & Tricks.

Tagged with , , .

Lessons Learned: To be a tournament player is not easy

Last night, I played in the final match of a bowling tournament held among companies. It was an 8-week effort and our team was played well until the final match. We completed our group as the leader after lots of challenging matches.

Everything was normal before the match. I was feeling good and ready to be a golden medalist. All of our company’s directors, my team leader, lost of colleagues and my best friends came to support me and our team. However, the night turned into a nightmare for me.

I played terrible and we lost. What was the mistake I did?

  • Lose my concentration: I was under heavy pressure and lost my concentration. The audience was waiting strikes from me and I was thinking about them not my game. It was the final. There was no return. I knew it, however I made panic. I could not control my excitement and could not play my real game.
  • Cannot adapt myself: All my shots were going over the second arrow and they were missing the first pin all the time. I could not change my start position or the bowl’s hitting point to overcome this. I don’t know why.
  • Trusting myself: I did not trust myself. I always started walking in hesitation. I did not think my steps, my direction or the hitting point. My spare performance was under %30. I could not see the line that takes bowl to the pin. Bowling is a mental game. It starts in your brain. Last night, I forgot about it.
  • Lack of practicing: Practicing practicing practicing. It makes you comfortable. You can say: “I can do it. Because, I did it 1000 times before”.

So, to be a tournament player is not easy. Do not limit them for bowling or other sports. IMHO, they can be applicable to the whole life.

I need to think about them for the next time and of course I’ll keep on working in this period. For now, it is good to be a silver medalist. :D

Posted in Self Improvement.

Tagged with , , , .

Success is next to you if…

I have recently watched the movie Kung Fu Panda. The movie is great and enjoyable. I think, it is one of the “must see” movies ever.

The story is about a lazy and fat panda called Po. His dream is to be a “Kung Fu” master even if it seems impossible. Nobody belives that Po can save their village and defeats the treacherous leopard Tai Lung. He cannot be the “Dragon Warrior”. However, everybody is wrong. Po’s dreams come to real when he noticed the “idea” and he succeeded.

In fact the “idea” is simple:

  • Belive yourself before you start anything
  • Work hard for the success with your own methods
  • Do not lose your passion and belief ever

You can do what ever you want as Po did. The thing is discovering the power inside you and releasing it…

Posted in Self Improvement.

Tagged with , .