0

What is the difference between these two functions in luajava:

-- I know this creates a new instance of a class
local instance = luajava.newInstance("path.to.class");
-- But it seems this does as well?
local class = luajava.bindClass("path.to.class");

Strangely enough I couldn't find any definitive answers to this. Most websites that use these functions do not bother describing the difference, and I seem to be able to call the same methods from both instance and class.

Charanor
  • 810
  • 8
  • 23

1 Answers1

0

bindClass can only access static members and doesn't create an instance of the object. This is useful for accessing static fields and methods, or for accessing enums. newInstance takes arguments and actually constructs a class which can have it's own non-static fields and methods.

Attempting to bind a class with non-static members may yield the following error.

Exception in thread "main" org.luaj.vm2.LuaError: @your_lua:<line_number> vm error: java.lang.IllegalArgumentException: Can not set <type> field your.package.YourClass.yourLocalField to java.lang.Class

As far as I see from some testing, both can only access public fields and methods, protected and private are hidden from lua.

D3_JMultiply
  • 1,022
  • 2
  • 12
  • 22