Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Import same class globally

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?

like image 635
BAD_SEED Avatar asked Oct 17 '11 08:10

BAD_SEED


People also ask

Do I need to import classes from the same package?

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).

Can we use * to import everything in class than specific class?

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.

Can you import multiple classes in Java?

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.

Can classes be imported in python?

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.


1 Answers

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.

like image 173
Egon Avatar answered Oct 01 '22 21:10

Egon