I have this method:
public void testJSNI2(){
String x = "test";
}
I can access this method like this:
[email protected]::testJSNI2(Ljava/lang/String;)
But how can I access the String x, which is defined inside a method?
The answer is not correct. JavaScript and Java dose not act the same. Person can access any field from js with the help of JSNI:
public class JSNIExample {
String myInstanceField;
static int myStaticField;
void instanceFoo(String s) {
// use s
}
static void staticFoo(String s) {
// use s
}
public native void bar(JSNIExample x, String s) /*-{
// Call instance method instanceFoo() on this
[email protected]::instanceFoo(Ljava/lang/String;)(s);
// Call instance method instanceFoo() on x
[email protected]::instanceFoo(Ljava/lang/String;)(s);
// Call static method staticFoo()
@com.google.gwt.examples.JSNIExample::staticFoo(Ljava/lang/String;)(s);
// Read instance field on this
var val = [email protected]::myInstanceField;
// Write instance field on x
[email protected]::myInstanceField = val + " and stuff";
// Read static field (no qualifier)
@com.google.gwt.examples.JSNIExample::myStaticField = val + " and stuff";
}-*/;
}
You can see this here: http://www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html
You can't access the variable x because it is in the scope of the method, the same way you wouldn't be able to access it in Java code.
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