B4J Question Private or shared public xui and jfx

Chris2

Active Member
Licensed User
Longtime User
A pretty basic question I think....

In large projects with multiple classes and code modules, for objects such as XUI & JFX, is it better for...

a) all classes/modules to share a single instance declared in one place as Public,

b) each class/module to have its own Private instance declared,

c) or does it make little/no difference?

Thanks.
 

William Lancee

Well-Known Member
Licensed User
Longtime User
As above by @EnriqueGonzalez
In addition:
If they are globals , you have to prefix their use each time by the global class/model name - an extra inconvenience.
In terms of memory and speed, there is no discernable difference after compilation.
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
They don't get their own instance even if you declare them in individual modules as they are to all intents and purposes static classes as far as B4x is concerned, think code modules. Otherwise the XUI.DefaultFolder couldn't work as it does. Declare them where you need them.
 
Last edited:
Upvote 1

Erel

B4X founder
Staff member
Licensed User
Longtime User
They don't get their own instance even if you declare then in individual modules as they are to all intents and purposes static classes as far as B4x is concerned, think code modules. Otherwise the XUI.DefaultFolder couldn't work as it does. Declare them where you need them.
That's 100% true.
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Thanks everyone.
think code modules
Yep, that makes it make sense to me ?. In effect we're kind of just adding a local reference to the same code module.

Otherwise the XUI.DefaultFolder couldn't work as it does
Oh yeah! Good point.

Just to prove it to myself....
Main:
Sub Process_Globals
    Private MainForm As Form
    Private Button1 As B4XView
    
    Private fx As JFX
    Private xui As XUI
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
    
End Sub

Sub Button1_Click

    xui.SetDataFolder("MadeUpFolder")
    
    Log(MyModule.xui.DefaultFolder)
    
    For i=0 To 5
        Dim mc As MyClass
        mc.Initialize
        Log(mc.MyXUI.DefaultFolder)
    Next
        
End Sub
MyModule:
'Static code module
Sub Process_Globals
    Public xui As XUI
End Sub
MyClass:
Sub Class_Globals
    Private xui As XUI
End Sub

'Initializes the object. You can add parameters to this method if needed.
Public Sub Initialize
    
End Sub

Public Sub getMyXUI As XUI
    Return xui
End Sub
Logs:
WARNING: package com.sun.javafx.embed.swing.oldimpl not in javafx.swing
Waiting for debugger to connect...
Program started.
C:\Users\Chris\AppData\Roaming\MadeUpFolder
C:\Users\Chris\AppData\Roaming\MadeUpFolder
C:\Users\Chris\AppData\Roaming\MadeUpFolder
C:\Users\Chris\AppData\Roaming\MadeUpFolder
C:\Users\Chris\AppData\Roaming\MadeUpFolder
C:\Users\Chris\AppData\Roaming\MadeUpFolder
C:\Users\Chris\AppData\Roaming\MadeUpFolder
 
Upvote 0
Top