Why cast an object type to null?

greetings. i doubt this is specifically related to swing but when i was looking at the source for JMenuItem i made an observation. In the first two constructors below "(Icon)" is cast onto null in the call to ini(). The last constructor does not. Is there any advantage or reason for casting onto null? thanks. jerry.

public JMenuItem(){

this(null, (Icon)null);

}

public JMenuItem(String text){

this(text, (Icon)null);

}

public JMenuItem(String text, Icon icon){

setModel(new DefaultButtonModel());

init(text, icon);

initFocusability();

}

public JMenuItem(String text,int mnemonic){

setModel(new DefaultButtonModel());

init(text,null);

setMnemonic(mnemonic);

initFocusability();

}

[1399 byte] By [crasypantza] at [2007-10-3 3:04:05]
# 1

I think, that in the first to that casted null to (Icon) so that the constructed that will be called is the

public JMenuItem(String text, Icon icon) {

and not

public JMenuItem(String text, int mnemonic) {

because both have a string as there first parameter the only distiction between them is the 2nd parameter. The last constructor its calling a method that doesnt have similar method parameters init(text, null); if there was another string, something, theyed have to.

I didnt check if there was another set of parameters similar just guessing.

E-Rocka at 2007-7-14 20:54:04 > top of Java-index,Desktop,Core GUI APIs...
# 2

since an int can't be null (try setting int to null in code and you'll get an error) i don't think that's the reason. you did make a good point that it would be used to differentiate between data types that could have a null value. perhaps they're covering the bases in advance for a future constructor with an Object that could be null.

crasypantza at 2007-7-14 20:54:04 > top of Java-index,Desktop,Core GUI APIs...