Thomas Thrien

Greenhorn
+ Follow
since Mar 05, 2008
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Thomas Thrien

Originally posted by Bill Shirley:
All unimportant to using Java.



As the invocation of static bound methods is faster than the invocation of dynamically bound, knowing about this stuff is important for using Java. Ok, I have to confess, not for the every day problems that even could be solved in reasonable (run)time with - let's say - Groovy.

But for code that is executed for every transaction on a busy application server a little final in front of a method may make the difference between success and failure.
16 years ago
For non-simple programs that's nearly impossible. The response above shows how to determine which classes are used during one single invocation; another invocation with other parameters may need other classes because other functionality is used.

Analyzing the code using reflection is also not feasible if advanced features are used (where advanced features start already with stuff like XML and JDBC).

JDBC is a good sample to explain why this causes problems: The old way to configure program with the JDBC driver was to call Class.forName( "JDBCDriverClass" ). Obviously there is no reference to this class in the binary code of that program. And when the name of the JDBCDriverClass is not hardcoded but read from somewhere, the program will never have any static reference of any kind to the jar holding the JDBC driver.

Similar scenarios can be found for XML parsers, Script engines, JNDI providers, Security providers, Encryption providers, Codecs, URL protocols ... I bet I missed a few ...

This also shows that different sessions with a program may use different classes from different jars: just assume a program like Squirrel that can be used to visualize different RDBMS databases.

On the other hand, if you have a simple, straight-forward program that does not use any kind of runtime plug-in mechanism, it could be possible to determine the really necessary jars: when you compile start with an empty CLASSPATH and add the jars until the compiler does not complain any longer.
This can be done also with running the program, but you may get surprised if someone uses a function that was not included into your tests - and now causes a CNF-Exception. But again, this will only work for really straight-forward programs.
16 years ago
Back to the original question ...

As far as I got it will any cast to a generic type trigger the unchecked warning as the compiler cannot check if it is correct (so it remains unchecked ...).

So there is no other way to get rid of the warning than to use to @SuppressWarnings annotation.

But applying it to the method at a whole is usually not a good idea if the method is a bit longer than just a few lines - although this is the way Eclipse is doing it for you ...

Instead the annotation should be assigned to the location where the cast is performed.

Assume the following code sequence:

public List<String> method()
{
return (List<String> getList();
}

The return statement will cause the warning that can be suppressed by

@SuppressWarnings( "unchecked" )
public List<String> method()
{
return (List<String> getList();
}

But when later code will be added to the method (perhaps additional casts ...), no warning will be thrown. So my recommendation is to write it as follows:

public List<String> method()
{
@SuppressWarnings( "unchecked" )
List<String> retValue = (List<String> getList();
return retValue;
}

Now only the warning caused by the assignment to retValue is suppressed - for the price of an additional local variable. Unfortunately the annotation can only be assigned to declarations, so the code below does not work:

public List<String> method()
{
List<String> retValue = null;

@SuppressWarnings( "unchecked" )
retValue = (List<String> getList();//Does not work!!!

return retValue;
}

In this case another helper variable might be necessary.

When the coder assigns the @SuppressWarnings annotation somewhere she makes a statement: it documents that he knows that the code sequence is somehow problematic, AND that he has taken appropriate precautions against any error conditions (if necessary at all). This Collection.emptyList() method is exactly that: as the list is empty it is ensured that is compatible to any type of List (as the generic "type" of a list is in fact the type of the List's contained elements).
16 years ago
First, the main feature of a Singleton is that it exists exactly once. So usually cloning does not make much sense. But as "once" is still relative, one can imagine scenarios where cloning of a Singleton is necessary.

But next: without the error message and the code for the Singleton class it is nearly impossible to determine the reason for the fail of clone().
16 years ago
After waiting for the SCEA5 results for weeks now, I finally found an entry in the Prometrics database that I failed part III. Ok, shit happens.

But I would really like to know, WHY I failed that part of the exam. Otherwise repeating it would not make any sense. And, to be honest, I doubt that it is part III that I failed; I assume it should read part II, as part III was more or less an attempt to ensure that it was really me who prepared part II.

So my question: did anyone failed the exam, too, and got some email, letter, whatever, that provides some information, why the test was failed?

Regards
thr