Make a new Java project in IntelliJ Idea and add the following files. Try to
compile them.
1.
public class Test {
// Variable name is the same as Class name!!!
static String String;
public static void main(String[] args) {
System.out.println(String.valueOf('c'));
}
}
2.
import java.util.ArrayList;
public class Test2 {
ArrayList list = new <String>ArrayList<String>();
}
In the first example IntelliJ gives a warning: "Static member ... accessed
via instance reference", however Sun compiler doesn`t show anything.
In the second example Intellij doesn`t show anything, hovewer Sun`s compiler
points out a BUG.
I work on IntelliJ 5.1.2. Are these problems present in another IDE-s?
Are these true BUGS or is there something with my brain :D
Jujo
> Make a new Java project in IntelliJ Idea and add the following files.
> Try to
[quoted text clipped - 22 lines]
> accessed
> via instance reference", however Sun compiler doesn`t show anything.
This is exactly what I would expect to see. The IDE has a lot of warnings
that are not present in the compiler. You can disable the inspections if
you don't like them. The reason you get a warning is because the "String"
in the main method is the variable name, not the String class. Calling a
static method in this way is confusing and probably not what the
programmer intended, hence the warning. It's not wrong in the strictest
sense, so the compiler doesn't complain.
> In the second example Intellij doesn`t show anything, hovewer Sun`s
> compiler
> points out a BUG.
This one does look to be a bug in the IDE. Have you tried one of the 6.0
builds to see if it has been fixed? Perhaps you should file a bug report.
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
£ukasz Krawczyk - 16 Nov 2006 09:57 GMT
>> In the first example IntelliJ gives a warning: "Static member ...
>> accessed
[quoted text clipped - 7 lines]
> programmer intended, hence the warning. It's not wrong in the strictest
> sense, so the compiler doesn't complain.
I agree, I didn`t realize that was the inspection, not compiler :)
But are You sure that this method is REALLY accessed, in this particular
case, via instance reference and NOT class name?
I can`t figure out the test to check this...
Even if String String = null, static methods of 'String' Class still can by
called, and not crash with an NullPointerException.
Jujo
Daniel Dyer - 16 Nov 2006 10:49 GMT
>>> In the first example IntelliJ gives a warning: "Static member ...
>>> accessed
[quoted text clipped - 17 lines]
> case, via instance reference and NOT class name?
> I can`t figure out the test to check this...
If you change the type of the variable called String to be Object:
Object String = null;
The compilation will fail because there is no valueOf method in the Object
class.
String.valeOf('c'); // Fails to compile, "cannot find symbol".
> Even if String String = null, static methods of 'String' Class still can
> by
> called, and not crash with an NullPointerException.
Doesn't matter, the field name is just used (instead of the class name) to
qualify the static method name. So this still works without a
NullPointerException:
public class Test
{
private String myString = null;
public static void main(String[] args)
{
new Test().doStuff();
}
private void doStuff()
{
System.out.println(myString.valueOf(76));
}
}
Dan.

Signature
Daniel Dyer
http://www.dandyer.co.uk
Daniel Dyer - 16 Nov 2006 23:35 GMT
>> 2.
>> import java.util.ArrayList;
>>
>> public class Test2 {
>> ArrayList list = new <String>ArrayList<String>();
>> }
...
>> In the second example Intellij doesn`t show anything, hovewer Sun`s
>> compiler
[quoted text clipped - 3 lines]
> 6.0 builds to see if it has been fixed? Perhaps you should file a bug
> report.
This is still an issue in IDEA 6.0.2.
Dan.

Signature
Daniel Dyer
http://www.uncommons.org
Daniel Pitts - 17 Nov 2006 00:04 GMT
> >> 2.
> >> import java.util.ArrayList;
[quoted text clipped - 20 lines]
> Daniel Dyer
> http://www.uncommons.org
It may be a bug, but it doesn't come up that often, does it?
Usually the generic type can be infered by the parameters, and AFAIK,
Idea handles this well enough, and its not really invalid syntax, so I
can understand why they missed it.