JAVA OBJECTS AND JAVASCRIPT

Posts

Pages: 1
For those of you that know java, I'm somewhat new to it, but I know a pretty good amount of javascript. I'm trying to use javax.script to eventually create an applet reimplementation of Sphere. My problem is, I can't seem to create javascript objects out of the java objects. I believe I have to do something like
ScriptEngine engine = new Scriptengine();

engine.put("javascript_object_name",java_object);
to create a javascript object from a java object. Problem is, I can't seem to even reference the methods I need to create javascript versions of.
I'm doing something like

(main file)
package JavaSphere
import javax.Script.*;

public class JavaSphereMain {
public void main() { //I'm making a command prompt example until I get this figured out, that's why I'm using main() instead of init()
//...
Functions f = new Functions();

engine.puts("js_obj",f.function_name);
}
}
(Functions.java)

public class Functions() {
private static void function_name() {
//do stuff
}
}

I guess my problem is I don't understand the public/private static/final/etc method usage
This?

You should use this syntax since you are using inheritance
public class JavaSphereMain extends Functions {
final - All data members have constant values.
static - All instances share the same member.

"Functions" should not have a () in front of it in class definition since it is a class and not a method. Also you have used "puts" instead of "put" in the second code.
I think you are using "put" function to assign a string "js_obj" to f.function_name(?) however, function_name is a method so I am not sure if it is a valid statement. To use the function_name method you should use:
f.funtion_name();
or probably use this instead
engine.put("js_obj",f);
(I am not sure what is meant by java_object in the first snippet of code, but I think "f" should be used since it suits the description of "java_object" instead of f.function_name.

My memory is very sketchy because its been nearly 3 years since I worked on java, and knew only the basics then (up till basic OOPs). I don't even know if what I've written is even related to your question, since you're using Sphere and javascript, but I hope this helps in some way. :)
You can't access function_name because it is private, a private method or member can only be access from within the class. Being public gives outside objects full influence over the public item. For example, if you want to make a member of a class read only you could do this:

public class SomeClass {
  private int member;
  public int getMember() {
    return member;
  }
}
member is hidden from all other objects as it is marked private but since getMember is public it can be accessed. A third keyword for this is protected. This makes a methods/attributes hidden from outside objects *except* classes that extend the superclass. So new example!
public class SuperClass {
  private int member;
  protected int anotherMember;
}

public class ExtendedClass extends SuperClass {
  public int getMember() {
    return member;
  }
  public int getAnotherMember() {
    return anotherMember;
  }
}
This will fail because ExtendedClass doesn't have access to member as it is a private member of SuperClass. getAnotherMember works find though as ExtendedClass does have access to the protected member anotherMember.

*edit*
used mod powers to set code tags to use java highlighting
author=LivingEffigy
final - All data members have constant values.
static - All instances share the same member.


THANK YOU! I could never understand what the difference was.
author=LivingEffigy
engine.put("js_obj",f);

that doesn't work either because if I try to call the method, I get an error saying that the method I'm trying to use in class f is sun.org.mozilla.javascript.internal.Native
JavaObject.


Never mind, I figured it out. I found some code that takes all the members in a class, and creates global Javascript functions.
Pages: 1