Previous | Next | Trail Map | The Basics | Naming Operations

Looking up an Object

To look up an object from the naming service, use Context.lookup()(in the API reference documentation) and pass it the name of the object that you want to retrieve. Suppose that there is an object in the naming service with the name "report.txt". To retrieve the object, you would write
Object obj = ctx.lookup("report.txt");

The type of object that is returned by lookup() depends both on the underlying naming system and on the data associated with the object itself. A naming system can contain many different types of objects, and a lookup of an object in different parts of the system might yield objects of different types. In this example, "report.txt" happens to be bound to a file (java.io.File). You can cast the result of lookup() to its target class.

For example, the following code looks up the object "report.txt" and casts it to File.

import java.io.File;
...
File f = (File)ctx.lookup("report.txt");

The complete example is in the file Lookup.java.


Previous | Next | Trail Map | The Basics | Naming Operations