B4J Code Snippet Sneaky JavaObject

Sometimes you just need to use a JavaObject to get a value to log for example. ( Be aware it makes the code harder to read using the tips below)

Normally you would write
B4X:
...
Dim jo As JavaObject
Log( jo.InitializeStatic("some.java.class").RunMethod("someJavaMethod",Null) )
...

You can get the same result without having to create a new JavaObject - just cast Null to JavaObject

B4X:
...
Log( (Null).As(JavaObject).InitializeStatic("some.java.class").RunMethod("someJavaMethod", Null) )
...

Or create a JavaObject on it's Dim line
B4X:
...
Dim myVar As JavaObject = (Null).As(JavaObject).InitializeStatic("some.java.class")
myVar.RunMethod("someMethod",Null) 
..

Use at your peril lol.
 

stevel05

Expert
Licensed User
Longtime User
It might be safer to use any other object you have to hand rather than Null, even an empty string "".As(Javaobject). Although it's probably just in my mind.
 
Top