Android Question Calling Java Class

iCAB

Well-Known Member
Licensed User
Longtime User
Hi All

Assuming that I have the following Java class

B4X:
#if JAVA
public class TestThis{

    public String ReturnString( String Value )
    {
        return Value;
    }
}

#end if


What is the proper way of calling ReturnString from with a B4A class or a code module

Thanks
iCAB
 

jahswant

Well-Known Member
Licensed User
Longtime User
What is the proper way of calling ReturnString from with a B4A class or a code module
Check JavaObject
Also check the Java Inline Tutorial.
B4X:
#If JAVA
public String FirstMethod() {
   return "Hello World!";
}
#End If

Sub Process_Globals
   Private NativeMe As JavaObject
End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
     NativeMe.InitializeContext
   End If
   Dim s As String = NativeMe.RunMethod("FirstMethod", Null)
   Log(s) 'will print Hello World!
End Sub
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Check JavaObject
Also check the Java Inline Tutorial.

Hi @jahswani thanks for your reply. I greatly appreciate it!

I actually tried to check the tutorials before I posted the question, and I did see syntax similar to the sample code you provided above.
The difference between what I am looking for and what you posted is this:

I want to be able to call the code from within a code module or class, but I wasn't sure what is the proper way to initialize the object and reach the method inside the java class

Thank you
iCAB
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I want to be able to call the code from within a code module or class, but I wasn't sure what is the proper way to initialize the object and reach the method inside the java class
See the inline java tutorial as already suggested!

- See the attached project. It shows how to call Java methods in all types of modules.
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
See the inline java tutorial as already suggested!
Hi Don, I did look at the sample code. I don't see an example of calling a Java Class from a B4A class or module.

Does the in-line Java code works for the above, or do I have to use the Java Object? (Also I was unable to find an example showing this )
 
Upvote 0

iCAB

Well-Known Member
Licensed User
Longtime User
Java Inline and JavaObject works every where.

I don't have any issue getting the code to work when I remove "Public class"... and call ReturnString directly.
I am just wondering about the syntax to call the function within the class from a B4A class.

B4X:
#if JAVA
public class TestThis{

    public String ReturnString( String Value )
    {
        return Value;
    }
}

#end if
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
I am just wondering about the syntax to call the function within the class from a B4A class.
Requires JavaObject library:
B4X:
Sub Class_Globals
'    Private fx As JFX
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    Dim jo As JavaObject = Me
    'Note#1: The class string for InitializeNewInstance is composed of
    '1) Package name: in this test case b4j.example
    '2) The name of the class module in B4A in lowercase. In this case the class module
    '   name is Test, but since B4A lowercases module names it becomes: test
    '3) The name of the class defined in the #if JAVA section (case sensitive): TestThis
    'Note#2: You could have a constructor declared. The call here would call one with
    ' no parameters
    jo.InitializeNewInstance("b4j.example.test.TestThis", Array(Null))
    Log(jo.RunMethod("ReturnString", Array("Hello from class in class")))
End Sub

#if JAVA
public class TestThis{

//  It is not necessary to declare a constructor, but one could. If you have
//  parameters you would need to change the Array(Null) section of the above
//  call to InitializeNewInstance method.
//  public TestThis() {}

    public String ReturnString( String Value )
    {
        return Value;
    }
}

#end if
 
Upvote 0
Top