Simplify Java Class Names Using import
Function
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:
java.lang.String
java.util.Enumeration
A fully qualified name can be long, making commands and functions (such as constructors) cumbersome to edit and to read. To refer to classes by the class name alone (without a package name), first import the fully qualified name into MATLAB.
MATLAB adds all classes that you import to a list called the import
list. To see what classes are on that list, type import
. 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 platform.
For example, suppose that a function contains these statements:
import java.lang.String import java.util.* java.awt.* import java.util.Enumeration
The following statements 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 remove the list of imported Java classes, type:
clear import