Friday, August 7, 2009

Tips to learn Java easy way


Why we can not declare constructor as final?

The keyword final when dealing with methods means the method cannot be overridden. Because constructors are never inherited and so will never have the opportunity to be overridden, final would have no meaning to a constructor.

What’s the difference between AWT and Swing?

There are no big architectural differences; the class hierarchy is almost the same. The reason is that Swing is built upon AWT. The most significant difference is how the components are drawn to the screen. AWT is so called heavyweight components and have their own viewport which sends the output to the screen. Swing is lightweight components and does not write itself to the screen, but redirect it to the component it builds on. Heavyweight components also have their own z-ordering. This is the reason why you can't combine AWT and Swing in the same container. If you do, AWT will always be drawn on top of the Swing components. You can combine AWT and Swing, just don't do it in the same container (e.g. panel, groupbox, etc.) and don't put a heavyweight component inside a lightweight. Another difference is that Swing is pure Java, and therefore platform independent. Swing looks identically on all platforms, while AWT looks different on different platforms.

Difference between Java beans and EJB?

Javabeans are much smaller than EJBs. You can use Javabeans to assemble larger components or to build entire applications. Javabeans, however are development components and are not deployable components. You typically do not deploy a Javabean rather, Javabeans help you construct larger software that is deployable. And because they cannot be deployed, Javabeans do not need to live in a runtime environment. Since Javabeans are just java classes they do not need an application server to instantiate them, to destroy them, and to provide other services to them. The application itself is made up of Javabeans.

Is it possible to change delays that affect appearing, keeping and disappearing of tooltip?

The ToolTipManager is a service class that maintains a shared instance registered with AppContext. We can access the ToolTipManager directly by calling its static sharedInstance() method:
ToolTipManager toolTipManager = ToolTipManager.sharedInstance();

Internally this class uses three non-repeating Timers with delay times defaulting to 750, 500, and 4000. ToolTipManager uses these Timer’s in coordination with mouse listeners to determine if and when to display a JToolTip with a component’s specified tooltip text. When the mouse enters a components bounds ToolTipManager will detect this and wait 750ms until displaying a JToolTip for that component. This is referred to as the initial delay time.

A JToolTip will stay visible for 4000ms or until we move the mouse outside of that component’s bounds, whichever comes first. This is referred to as the dismiss delay time. The 500ms Timer represents the reshow delay time which specifies how soon the JToolTip we have just seen will appear again when this component is re-entered.

Each of these delay times can be set using ToolTipManager’s setDismissDelay(), setInitialDelay(), and setReshowDelay() methods.

ToolTipManager is a very nice service to have implemented for us, but it does have significant limitations. When we construct our polygonal buttons we will find that it is not robust enough to support non-rectangular components.

Is it possible to stop an object from being created during construction?

Yes, if the constructor throws an exception. Formally, an object will be created (since the constructor is a method invoked after the actual method creation), but nothing useful will be returned to the program, and the dead object will be later reclaimed by Garbage Collector.

The other way is when you leave calls to the constructor to a static factory method which can check the parameters and return null when needed.

Note that a constructor - or any method in general - throwing an exception will not "return null", but will leave the "assign target" as it was.

No comments: