One of my biggest frustrations in researching examples, reading tutorials, etc. - which I'm doing a lot of in trying to learn Java ... is that they almost never seem to tell you what packages(?) need to be imported to run the code they are showing. And even if they do, it's difficult (for me at least) to understand where a class/method is coming from. I'll see something like FooClass or fumMethod doing stuff, while having no idea where it is defined? Is this just a matter of grasping this stuff by osmosis over time, or am I missing some simple way to divine this information?
Related to this - and perhaps this is a NetBeans specific question, is there a way to "Switch to" the class/method definition/file by highlighting a class or method name and typing some key shortcut or something? Also - is there anyway to highlight a keyword or class name (for the base java classes I guess) and get some sort of language reference? In fact - is there language reference at all anywhere? I'm talking something like this (from SQL Server documentation - I pulled this at random):
Ken Matson wrote:or am I missing some simple way to divine this information?
Alternatively, if I know the class, I may search the JavaDocs, assuming I know what source the package comes from, can locate its JavaDocs (assuming there are some - which most of the reputable systems do have).
Seriously, I too find it quite annoying when people display code snippets with insufficient context. You are not alone.
I can't speak for NetBeans these days, but it's not uncommon to be able to right-click on a word and bring up a context menu that can do things like jump to source or display a popup JavaDoc snippet. Sometimes holding the Ctrl or Alt keys helps.
An IDE is no substitute for an Intelligent Developer.
Tim said already, the main source about the Standard Java Library is in JavaDocs (<-- click). These docs are for JDK9. There is one feature introduced since Java 9 came out, that they also created a search functionality within the docs - that's very helpful. So what you do, is you type class name or method name and if that is part of standard library, for sure you'll find there. That information will contain information API's user needs. You won't see implementation of them - but you don't need to. In case you want to have a look at actual source code, you can navigate to where Java is installed and unpack src.zip file and there you'll find source code. Or for a quick sneak you can look to classes implementation from within the IDE, at least IntelliJ IDEA can do that (ctrl+b or cmd+b) as it unpacks src.zip file itself under the hood. I'm quite certain Netbeans also can do something similar, just don't know the shortcut.
Ken Matson wrote:Related to this - and perhaps this is a NetBeans specific question, is there a way to "Switch to" the class/method definition/file by highlighting a class or method name and typing some key shortcut or something? Also - is there anyway to highlight a keyword or class name (for the base java classes I guess) and get some sort of language reference? In fact - is there language reference at all anywhere?
I expect there is -- both of those things exist in Eclipse so Netbeans should have them too. I copied this thread to the Netbeans forum so maybe somebody reading it there will know the answer.
* When you've copied and pasted code that has no imports: click the class and press Alt-Enter (I believe it has to be the left Alt key). This will give you suggestions as to which imports you should add.
* When you've not sure where a method is defined: right-click the method and select Navigate. It will show you many useful tools. Ctrl-B goes to the declaration and Ctrl-Shift-B goes to the source code.
All things are lawful, but not all things are profitable.