2

Can I refer to a specific package in java with a variable name?

Say I want to access a method foo() in a nested structure at stuff.biscuit.jelly.sausages.foo(), (where jelly is a package and sausages is a class)

can I set a variable var such that var = stuff.biscuit.jelly.sausages, and then access foo() via:

var.foo() ? (or something vaguely similar)

edit:

regarding imports, any recommendations if i want to use lots of different foo() methods, such as:

stuff.biscuit.jelly.cherry.foo()
stuff.biscuit.jelly.apple.foo()
stuff.biscuit.jelly.strawberry.foo()

?

edit: the proposed duplicate is unrelated. it's talking about using the same name for a variable as a package. i'm talking about referring to a nested package with a variable.

stuart
  • 1,785
  • 2
  • 26
  • 38
  • 1
    Nope. You can't! You can reduce your work by importing all classes in a package by saying `import package.*`; – Chetan Kinger Mar 20 '15 at 16:25
  • Sorry, I am not sure if "sausages" is supposed to be a class or if you are asking if you can call a method on a package... – Florian Schaetz Mar 20 '15 at 16:28
  • Either way. It does not work. – Murat Karagöz Mar 20 '15 at 16:29
  • 1
    If it was a class, he could use reflection to get a class object, call newInstance() and then even use reflection to call the method. Or cast to some interfac to call foo(). – Florian Schaetz Mar 20 '15 at 16:31
  • possible duplicate of [is there any restriction on variable name so that it should not conflict with package name?](http://stackoverflow.com/questions/28498915/is-there-any-restriction-on-variable-name-so-that-it-should-not-conflict-with-pa) – Prashant Mar 20 '15 at 16:48
  • 2
    @Prashant No, that question is about something else. – Philipp Wendler Mar 20 '15 at 17:10

2 Answers2

3

No you cannot do that. Best you can do is define your method as static and do a static import of that corresponding class. Then you can directly call that method.

Something like

import static stuff.biscuit.jelly.Sausages.*; 

and then in your code simply use

foo();
Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • 1
    You are in a pickle then. You will have to use fully qualified package name to call the method unless you use reflection technique. But unless it is absolutely necessary one should not use reflection. – Aniket Thakur Mar 20 '15 at 16:46
0

You can get a reference to a method using reflection, and call that method using invoke(...). Presuming 'sausages' is an instance of the Sausages class:

Class cl = sausages.getClass();
Method myMethod = cl.getMethod("myMethod"); 
myMethod.invoke(params);

Although when all is said and done, why not just import the package/class and call the method that way?

copeg
  • 8,290
  • 19
  • 28
  • because of multiple foo()'s in different classes in different packages – stuart Mar 20 '15 at 16:43
  • Then import the classes and call the methods on the class/instance level rather than doing a static import. Reflection might answer the initial question, but would not be a recommended way to go about accomplishing what it seems you wish to accomplish. – copeg Mar 20 '15 at 16:53