Main Content

SimplifyJavaClass Names UsingimportFunction

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 theimport list。To see what classes are on that list, typeimport。您的代码可以参考名单上的任何类class name alone.

When called from a function,importadds the specified classes to the import list in effect for that function. When invoked at the command prompt,importuses the base import list for your MATLAB platform.

For example, suppose that a function contains these statements:

importjava.lang.Stringimportjava.util.*java.awt.*importjava.util.Enumeration

The following statements refer to theString,Frame, andEnumerationclasses without using the package names.

str = String('hello');% Create java.lang.String objectfrm = Frame;% Create java.awt.Frame objectmethodsEnumeration% List java.util.Enumeration methods

To remove the list of imported Java classes, type:

clearimport

See Also

Related Topics