Archive for the 'Java' Category

Tags: , , , ,

5 Tips for Debugging Java Code in Eclipse

5 tips you can use while debugging your Java code in Eclipse.

  1. Logical Structure
    The logical structure is very handy for examining things like maps and other java collections classes, instead of showing the full detail of all the internal variables the view will only show the “logical” pieces. It is activated/deactivated from “Variables” view using logical structure toggle button.


  2. Conditional Debugging
    Once you know where an error occurs, you will want to see what the program is doing right before it crashes. One way to do this is to step through every statement in the program, one at a time, until you reach the point of concern. Sometimes it’s better to just run a section of code and stop execution at that point so you can examine data at that location. It’s possible to declare conditional breakpoints triggered whenever the value of an expression changes. In addition, code assist is available when typing in the conditional expression.

    Right click your breakpoint and select Breakpoint Properties… to set a condition. Check “Enable Condition” box and write your expression below it. You can define a “hit count” that stops your code after that count is reached if you want.

  3. Java Exception Breakpoint
    Use “Java Exception Breakpoint” feature to track your exceptions more easily. For example, your program might be throwing a NullPointerException or FileNotFoundException and you want to track it down. Rather than go to the line of code and set a breakpoint, you can specify that the debugger should always stop when the exception occurs.

  4. Step Filtering
    “Step Into” is one of the most used feature while debugging the source code. Sometimes, it takes us through several stack traces to reach the code we want to debug. In such cases, “Step with Filters” functionality can help us. It simply filters out the classes that we don’t want to step into.

    To specify which classes to filter out when “Step with Filters” is used, the preferences must be set. To activate the preferences panel choose Window > Preferences from the main window. Once there navigate to Java > Debug > Step Filtering and choose the classes/packages to be filtered out. When you step through the code, ensure that the “Use Step Filters” toggle button in Debug view is on.

  5. Expression Evaluation
    “Inspect” is a handy feature for evaluating your expressions. In the Debug perspective, select your expression, right click it and select “Inspect” from the menu to evaluate. You can do it by simply Ctrl+Shift+I key combination.

    “Display View” is another efficient helper in evaluating expressions. Activate it from Window > Show View > Display, write your expressions to evaluate and execute them by right clicking and selecting “Display” from the context menu in the Debug perspective or simply by Ctrl+Shift+D shortcut. You can use code assist (Ctrl+Space) to compose your expressions.

Happy debugging. :D


RSS Feed

Digg It!

Add to Del.cio.us

Stumble It!

Add to Technorati Favorites

Add to Reddit
Tags: , , ,

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:

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.


RSS Feed

Digg It!

Add to Del.cio.us

Stumble It!

Add to Technorati Favorites

Add to Reddit



View Abdullah Çetin ÇAVDAR's profile on LinkedIn

Join My Community at MyBloglog!

Add to Technorati Favorites

Subscribe To My FriendFeed!

My Facebook Profile

E-Mail Subscriptions

Enter your email address:

My Flickr Photos

www.flickr.com

My Recent Readers