Knute Snortum wrote:I think using import statements and short names in your code is the preferred way, but I'll post this to the IDE forum to get other opinions.
My opinion is that your opinion is correct.
As a C-to-Java programmer, I jumped to the false conclusion that "import" was Java's version of C's "#include" preprocessor directive (which may mean it isn't even part of C, but that's a topic for another
thread).
All
import does for you is allow you to refer to names defined within other packages, without having to qualify those names with their full package names. There are a mix of views on how best to use
import, but I would venture to say that everyone uses it at least some of the time. Where it can fail you is if you must refer to symbols in two different packages that have the same short names. Consider these three classes, each in a different package:
Line 8 in that third bit is going to cause a compiler error, because the compiler knows (owing to the
import statements at Lines 3 and 4) that
both the
thehill.jack package
and the
thehill.jill package define a symbol named
PailOfWater. The compiler has no way of knowing which one you mean. You can solve this problem by refactoring one of the two packages (but that would mean refactoring all their clients in the light-cone created since you first deployed the second package), or you can resolve the ambiguity with full qualification:
Note that, even though both packages are fully imported, there is no ambiguous reference in the code above, so the compiler happily tolerates the fact that a symbol of the same name is defined in two different, imported packages. Until it needs to find an unqualified symbol in an imported package, the compiler doesn't care if that symbol is defined more than once.
Note also that none of what I've said depends on
thehill.jack and
thehill.jill starting with
thehill., as it will apply if those packages were just named
jack and
jill, (I just named them the way I did so they'd all show up near each other in "Experiments" project package tree in NetBeans).
As for JFrame in particular, you will see that NetBeans loves to subclass JFrame to create your "main" window for you. This is usually harmless, but folks here persuaded me that it's not the best practice, and offers no meaningful benefits. Better is to create a JFrame directly, and put a JPanel into it that you use the NetBeans GUI editor to create. If you are just getting started, go ahead and use the JFrame subclasses that NetBeans creates for you. Just remember someday, if you can, that creating your own JFrame is probably what you ought to be doing.