Android Question Inline Java classes?

Discussion in 'Android Questions' started by JordiCP, Feb 20, 2015.

  1. JordiCP

    JordiCP Well-Known Member Licensed User

    I don't know if it has been posted somewhere, but at least I haven't been able to find it searching in the forum

    Can someone post a simple example (or link) of how to declare a simple class with inline Java and then use it from the B4A code? for instance a class with just one variable and one function.

    I have been trying with examples similar to THIS , which does not work, trying to change things, but with no succes.:(
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    The first question is:
    - Why do you want to declare a class?

    This will create an inner class within the Activity/Service/Class module. I dont see a point unless the class you want to declare is static. If its static you dont need to declare a class!
    NJDude and JordiCP like this.
  3. JordiCP

    JordiCP Well-Known Member Licensed User

    Well, the main purpose is learning, so it is a kind of exercise for myself. Also, I must recognize that I have done things with Java but still don't feel totally comfortable with it. :confused:

    Let's suppose I want to declare, fully in Java, an object "dot_in_a_line" with some members (variables and functions)

    int xpos; // not visible from the outside
    int step; // not visible
    init( x_ini,step_ini ) --> sets xpos=x_ini, step=step_ini
    goright() --> sets xpos+=step
    goleft() --> sets xpos-=step
    int getpos() --> returns xpos

    And then want to declare N instances of this object from b4a and use them.

    Usually I declare a class in B4A to achieve this and then I can instantiate as many objects as I want.
  4. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Lets say for e.g. you use inline java to create a class within an activity. That class can only be instantiated within the activity. It is in the parent class scope, it is not visible from anywhere else.

    Admittedly, I havent really tried to do it, and I am not seeing a strong use for it. Maybe someone else can shed some light on it.
    NJDude and DonManfred like this.
  5. JordiCP

    JordiCP Well-Known Member Licensed User

    Yes, the purpose is to use it from the same activity. And I agree that it can be perfectly accomplished by other means if it is needed.

    Anyway, my question is more related to the "is it possible to make it work correctly?" because I am getting errors

    For instance, if I declare a class "myclass" from Main and my packagename is "jcp.b4a.myexample", the only way I can get access to the class is

    Dim OO as JavaObject
    But then I face other runtime errors such as "no empty constructor found", even if I provide one....

    So I think Erel has the key to it.
  6. Erel

    Erel Administrator Staff Member Licensed User

    Can you post the code?
  7. JordiCP

    JordiCP Well-Known Member Licensed User

    Sub Process_Globals
    'These global variables will be declared once when the application starts.
        'These variables can be accessed from all modules.
        Private NativeMe As JavaObject
    Dim OO As JavaObject
    Dim Timer1 As Timer
    End Sub

    Sub Globals

    End Sub

    Sub Activity_Create(firsttime As Boolean)

    'Error --> no empty constructor en runtime
    Dim qqo As JavaObject
    Dim qq As Int=2

    'This gives--> no constructor found

    End Sub

    Sub Activity_Resume
    End Sub

    Sub Activity_Pause (UserClosed As Boolean)
    End Sub

    #If java

    class mylib{

        public mylib(){}

        //public mylib( int k){ pos=k;}
        int pos;
        public void go_right(int step){
        public void go_left(int step){


    #End If
    Elton Leung likes this.
  8. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Weird. I get
    java.lang.IllegalAccessException: class b4a.example.main$mylib is not accessible from class anywheresoftware.b4j.object.JavaObject
    JordiCP likes this.
  9. Erel

    Erel Administrator Staff Member Licensed User

    Change class mylib to public static class mylib.

    As I see it this is not the correct way to develop with B4A. It is easy to "overuse" this feature.
    The main purpose of inline java feature is to help with integration of third party libraries.
    JordiCP likes this.
  10. JordiCP

    JordiCP Well-Known Member Licensed User

    Thanks Erel, now it is running ok

    I understand what you say, but it helps me a lot to learn how it all works :)
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice