Java Question Access java class without declaration of a variable

Biswajit

Active Member
Licensed User
Longtime User
Hello,

Is it possible to write a class in Java that B4A IDE can detect as a keyword, like DateTime, Gravity, Colors, etc?
Which we can use without declaring a variable. like we write Gravity.CENTER not Dim g as Gravity and then g.CENTER

I checked the B4A keyword source code from GitHub. It seems those classes don't have a @ShortName assigned to them. I tried the same but the IDE is not detecting those classes when the XML file has the definition of the class and its variables.

Even in B4A we can create Static Code Module to achieve the same thing. There must be a way to do in Java.

Let me know if there is any specific process to do that.
 
Last edited:

drgottjr

Expert
Licensed User
Longtime User
this works for me:
create your little class and build with SLC


Java:
package com.georgieapps.numbers;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;

@Version(1.0f)
@ShortName("Numbers")
//@ActivityObject
public class Numbers {
    public String ONE = "one";
    public String TWO = "two";
    public String THREE = "three";
   
}

open your ide and create a test
search for new class in libraries tab. select it

declare your class in app:
B4X:
       Private numbers As Numbers

refer to class property without "declaring it" in your app:
B4X:
      Log(numbers.ONE)     '  just like GRAVITY.CENTER

' if you were thinking of creating the class in inline java, although
you can create and access a homegrown class (similar-ish to above),
i think the only way you can access it is via a javaobject ( runmethod,
getfield, etc). so it's not going to look like what you're trying to achieve
(although, in the end, you're still accessing the class' field).
 

Attachments

  • capture.png
    capture.png
    16.5 KB · Views: 89

Biswajit

Active Member
Licensed User
Longtime User
this works for me:
create your little class and build with SLC


Java:
package com.georgieapps.numbers;

import anywheresoftware.b4a.BA;
import anywheresoftware.b4a.BA.*;

@Version(1.0f)
@ShortName("Numbers")
//@ActivityObject
public class Numbers {
    public String ONE = "one";
    public String TWO = "two";
    public String THREE = "three";
 
}

open your ide and create a test
search for new class in libraries tab. select it

declare your class in app:
B4X:
       Private numbers As Numbers

refer to class property without "declaring it" in your app:
B4X:
      Log(numbers.ONE)     '  just like GRAVITY.CENTER

' if you were thinking of creating the class in inline java, although
you can create and access a homegrown class (similar-ish to above),
i think the only way you can access it is via a javaobject ( runmethod,
getfield, etc). so it's not going to look like what you're trying to achieve
(although, in the end, you're still accessing the class' field).

Thank you. But I know this method. Thats how I build wrappers. but accessing constants like this is not the correct way I think. I know the other ways also like you said inline java code.

Even in B4A we can create Static Code Module to achieve the same thing. There must be a way to do in Java.

If the IDE can detect the internal classes like that why not the additional / third-party classes? There must be a way for creating such classes. Though I'm not sure and thus asked for that.
 
Last edited:

Biswajit

Active Member
Licensed User
Longtime User
Not directly. If you are creating a b4xlib (which you should) then you can include a static code module in your library.
Ok. Thank you.
 
Top