.quote {
font-style: italic;
font-size: 1.1em;
font-weight: bold;
width: 300px;
}
.quote_left {
float: left;
padding-right: 3px;
}
.quote_right {
float: right;
padding-left: 3px;
}
In Java all classes are part of a package. This goes for the classes you write, classes from third-party libraries and the classes from the Java Development Kit (JDK). Think of a package in the words most litteral meaning – something that groups together one or more objects to make them easier to handle. The objects that we package are classes (and interfaces).
All classes for Notes/Domino are grouped together in a single package called “lotus.domino”. Since the name of a class is actually the name of the package combined with the name of the class, the “real”, fully qualified, name of the Document class (the equivalent of the NotesDocument class from LotusScript) is actually “lotus.domino.Document”. Apart from the “lotus.domino” package you will routinely use classes from the core Java development kit (JDK). The packages from the JDK all start with “java”.
“If you do not explicitly put a class in a package it will be part of what’s called the default package”
If you do not explicitly put your classes in a package they will be part of what’s called the default package. For most programming in Java in Notes/Domino there’s no need to worry about putting your classes in a package – they mostly come into play when writing classes for distribution (e.g. designing API’s).
Importing
When referring to classes in Java you need to use the fully qualified name (e.g. lotus.domino.Document). This will quickly become very cumbersome so you can tell the compiler which packages you use in your code and it will automatically insert the package names at compile time.
In Java-speak this is called “importing a package”.
“Importing packages” is telling the compiler which packages you are using in your code…
For instance by importing the lotus.domino package in Java agents you can simply refer to the Document class instead of the lotus.domino.Document class. This can save quite a lot of typing, it makes the code less verbose and easier to read.
The two agents below are exactly the same from the compilers point of view. The first example shows an agent that import the entire lotus.domino package using the asterix notation (line 1 in bold):
import lotus.domino.*; public class JavaAgent extends AgentBase { public void NotesMain() { try { Session session = getSession(); AgentContext ac = session.getAgentContext(); Database db = ac.getCurrentDatabase(); View view = db.getView("($All)"); Document doc = view.getFirstDocument(); while (null != doc) { // do stuff with the document... doc = view.getNextDocument(doc); } } catch(Exception e) { e.printStackTrace(); } } }
The second example shows the same agent but this time I didn’t import any packages. This means that I have to prefix each class with the name of its package. It is more verbose and harder to read.
public class JavaAgent extends lotus.domino.AgentBase { public void NotesMain() { try { lotus.domino.Session session = getSession(); lotus.domino.AgentContext ac = session.getAgentContext(); lotus.domino.Database db = ac.getCurrentDatabase(); lotus.domino.View view = db.getView("($All)"); lotus.domino.Document doc = view.getFirstDocument(); while (null != doc) { // do stuff with the document... doc = view.getNextDocument(doc); } } catch(Exception e) { e.printStackTrace(); } } }
Please note: The java.lang package is implicitly imported – always!
Drawbacks
However great this is, it also means that you must be aware when this approach will give you problems. Consider using and importing two packages that both contain a class of the same name. In this situation the compiler can’t figure out which class to use (from which package) so it wont compile the code. The solution here is to fully qualify the offending classes with the package names to explicitly tell the compiler which class you are referring to.
Shooting with smaller bullets
Apart from importing an entire package at a time by using the asterix notation (lotus.domino.*) you can also import a single class at a time. You do this by substituting the asterix with the name of the actual class (lotus.domino.Database, lotus.domino.Document etc.). This approach is the one automatically used in Eclipse and it helps alleviate the problem of class name collisions described above. The example above would then look like this:
import lotus.domino.AgentBase; import lotus.domino.Session; import lotus.domino.AgentContext; import lotus.domino.Database; import lotus.domino.View; import lotus.domino.Document; public class JavaAgent extends AgentBase { public void NotesMain() { try { Session session = getSession(); AgentContext ac = session.getAgentContext(); Database db = ac.getCurrentDatabase(); View view = db.getView("($All)"); Document doc = view.getFirstDocument(); while (null != doc) { // do stuff with the document... doc = view.getNextDocument(doc); } } catch(Exception e) { e.printStackTrace(); } } }
If you do not care about creating your own packages this is all you need to know about packages. If you do care about creating your own packages stay tuned for the next post on packages.