External Interfaces/API |
Simplifying Java Class Names
Your MATLAB commands can refer to any Java class by its fully qualified name, which includes its package name. For example, the following are fully qualified names:
A fully qualified name can be rather long, making commands and functions, such as constructors, cumbersome to edit and to read. You can refer to classes by the class name alone (without a package name) if you, first, import the fully qualified name into MATLAB.
The import
command has the following forms.
import pkg_name.* % Import all classes in package
import pkg_name1.* pkg_name2.* % Import multiple packages
import class_name % Import one class
import % Display current import list
L = import
% Return current import list
MATLAB adds all classes that you import
to a list called the import list. You can see what classes are on that list by typing import
, without any arguments. Your code can refer to any class on the list by class name alone.
When called from a function, import
adds the specified classes to the import list in effect for that function. When invoked at the command prompt, import
uses the base import list for your MATLAB environment.
For example, suppose a function contains the following statements.
Code that follows the import
statements above can now refer to the String
, Frame
, and Enumeration
classes without using the package names.
str = String('hello'); % Create java.lang.String object frm = Frame; % Create java.awt.Frame object methods Enumeration % List java.util.Enumeration methods
To clear
the list of imported Java classes, invoke the command
Loading Java Class Definitions | Creating and Using Java Objects |