I have a function file called getFeatures.m
that looks like:
function [features] getFeatures()
% Import the XPath classes
import javax.xml.xpath.*
% other code
end
function [name] = getName()
% Import the XPath classes
import javax.xml.xpath.*
% other code
end
As you can see, both functions import xpath library, since I have a lot of functions that need to import that class how can I do this a single time?
If you want to find static methods in the imported class, you should type class name the press dot(.) to trigger the proposal. Eg: YourImportedClassName. In Java, you do not need to add import statements for classes that exist within the same package (they are actually automatically “imported” by the compiler).
If you are importing more than 20 classes from the same package, you are better off using import xxx. *. "Clean Code" is in favor importing the whole package as well.
the compiler has no way of knowing which Addition class you are referring to. Therefore, if you have two classes with the same name then you can import only one and you will have to use the fully qualified name for the other.
It allows us to use functions and classes kept in some other file inside our current code. Python provides us with various ways in which we can import classes and functions using the import statements.
I have stumbled upon the same problem. My personal (and ugly!) workaround for this is defining a method that performs the imports; you will still have to call that function, but at least it groups the imports at a single place, albeit inside strings.
function cmd = initJava()
cmd = 'import package.*';
if nargout == 0
warning('off','MATLAB:Java:DuplicateClass');
evalin('caller',cmd);
warning('on','MATLAB:Java:DuplicateClass');
end;
end
This can be called either as initJava()
or eval(initJava())
. If I remember correctly the first one doesn't always do what it's supposed to do, but you'll have to test that yourself.
If someone has a better/nicer/other solution, I'm very interested in hearing that one.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With