Archive

Posts Tagged ‘java’

Response: Are Naming Conventions Still Needed For Abstract Classes?

September 16th, 2009 8 comments

I came across a post on the Adam Bien blog recently about using the prefix ‘Abstract‘ to define Abstract classes. The argument was that using the ‘Abstract‘ prefix is superfluous. I do not fully agree with the post. As I read the points he had mentioned on the post, here are some that came to mind

Abstract classes are already distinguishable by the keyword abstract. There is no need to further emphasize it.

A prefix “Abstract” doesn’t provide any additional value to the user – in contrary it blurs the actual intention

The Abstract in the class name helps a developer know that a class is Abstract even before they open it. Of course if some one bucks the convention and names a Concrete class abstract, it creates confusion but that scenario is rare.

Modern IDEs don’t let you instantiate an abstract class, even before saving / compiling.

Categories: java Tags: ,

Eclipse tips and tricks – Part 2

September 5th, 2009 7 comments

Eclipse can be used to debug your java applications. Here are a few tips that can help you debug better

Remote debugging:

The eclipse IDE can remote debug your web application. Imagine being able to debug your development server from your local machine. Eclipse can help you do this. First you need to instruct your application / server to listen on a port for debug messages. That can be done using the -Xdebug flag

java -Xdebug -Xrunjdwp:transport=dt_socket,server=y,address=8787,suspend=n

This tells the JVM to listen for specific debug messages on port 8787. In eclipse open Bug Icon -> Debug configurations and select “Remote java application”. Mention the port and server address and you should now be able to remote debug the application. Make sure that the classes and source sync with each other. On a network where the bandwidth is poor, this technique will not work.

Remote java application:

Categories: java Tags: , , ,

Eclipse tips and tricks – Part 1

August 30th, 2009 5 comments

Eclipse is a pretty good IDE to develop java apps on. However there are some features that you might not be using every day that can save you a lot of time.

Writing your own code templates is one of them. To write your own code template do the following

Go to Window -> Preferences. Select Java -> Editor -> Templates. You can add your own templates here. Templates are nothing but the code that auto completes when you press ctrl+space. For example type syso and ctrl+space. This will automatically fill in System.out.println() into the editor.

Eclipse code templates:
eclipse_code_template
Code template variables:

Eclipse can automatically fill out dynamic code by sniffing out variables in a template. Here are a few you should make yourself aware of

Categories: java Tags: , , ,

Your favorite java IDE

August 27th, 2009 42 comments

Which of these tools do you use to code in java ? I use myeclipse for J2EE coding and Netbeans for swing UI (which I would highly recommend.). I have worked with workshop as well.

Which of these is your favorite java IDE ?

  • Eclipse - J2SE / J2EE (48%, 1,405 Votes)
  • Netbeans (44%, 1,293 Votes)
  • IntelliJ (17%, 495 Votes)
  • Plain text editor (4%, 110 Votes)
  • Myeclipse (3%, 74 Votes)
  • JDeveloper (1%, 40 Votes)
  • RAD / WSAD (1%, 35 Votes)
  • Other (1%, 34 Votes)
  • Weblogic workshop (0%, 8 Votes)

Total Voters: 2,926

Loading ... Loading ...





Categories: java Tags: , ,

Java shutdown hooks

August 24th, 2009 4 comments

Java allows you to add shutdown hooks to your code. A shutdown hook is simply a thread that has been left in the initialized state. When your JVM is about to shutdown, the shutdown hook thread kicks in. The finalization processes of java objects run after the shutdown hooks complete. The JVM allows you to register more than one shutdown hook.

Let us take a look at how this is done

public class Task
{
    public static void main(String[] args)
    {
        Runtime runtime = Runtime.getRuntime();
        Thread thread = new Thread(new ShutDownListener());
        runtime.addShutdownHook(thread);
        someProcess();
    }
 
    private static void someProcess()
    {
        try
        {
            System.out.println("I am busy");
            Thread.sleep(2000);
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
    }
}
 
class ShutDownListener implements Runnable
{
    @Override
    public void run()
    {
        System.out.println("I am shutting down");
    }
}

The Task class is a simple class that runs a process. Once this process finishes up, we want the JVM to run a shutdown hook to notify us that the JVM is shutting down. When this program is run, the output is

I am busy
I am shutting down

Nice !

Lets experiment a little more. How robust is a shutdown hook ? After the someProcess() method is called let us analyze what will happen when one of the following statements are added

Categories: java Tags: ,