B4J Question B4J MyClass instance

avacondios

Active Member
Licensed User
Longtime User
Hi Erel,

I can create an instance of class without to execute the myClass.initialize. Is this a correct behaviour ? I though for a new instance of any object, you need to run the initialize function.

Without to run the initialize, are you creating a static class ? Please explain with details that.

BR/Antonis
 

avacondios

Active Member
Licensed User
Longtime User
Dear Erel,

I have created a sample class :

B4X:
'Class Demo
Private Sub Class_Globals
    Private xxxID As String
    Private xxxCAS As String
    Private xxxExpiry As Int
    Private xxxContent As String
End Sub

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


Public Sub getID As String
    Return xxxID
End Sub

Public Sub getCAS As String
    Return xxxCAS
End Sub

Public Sub getExpiry As Int
    Return xxxExpiry
End Sub

Public Sub getContent As String
    Return xxxContent
End Sub

Public Sub Demo
    Log("demo")
End Sub

from the Main module, I can call the following :

B4X:
    Dim mDemo As clsDemo
    mDemo.Demo


Without to execute the mDemo.initialize , the sub Demo executes (write on log the word demo)

That means, that the class instance is STATIC ?

Can you explain that ?
 
Upvote 0

BPak

Active Member
Licensed User
Longtime User
Initialize - A class Object should be initialized before you can call any other Sub.
Initializing an Object Is done by calling the Initialize Sub.
When you call Initialize you set the Object's context (the parent activity or service).
See CLASS TUTORIAL...
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
It is true, the instance of the class works perfectly well without initializing it. Even properties (i'm trying b4a).

B4X:
'MyClass - Class module
Sub Class_Globals
    Private mText As String
End Sub

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

Public Sub setText(Text As String)
    mText = Text
End Sub
Public Sub getText As String
    Return mText
End Sub

Public Sub WriteLog(Text As String)
    Log(Text)
End Sub


B4X:
Sub Activity_Create(FirstTime As Boolean)

    Private MyObj As MyClass
    MyObj.WriteLog("Ciao")
    MyObj.Text = "Erel"
    MyObj.WriteLog(MyObj.Text)
   
   
    Private MySecondObj As MyClass = MyObj
    MySecondObj.WriteLog(MySecondObj.Text)
   
    ExitApplication
End Sub
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
@avacondios You could test it, create two cases of your class, change something in one and see if it changes the other one, If it does its a static class.
eg,
B4X:
dim a as myclass ' with a variable text
dim b as myclass
a.text = "fred"
log(b.text)
if the log print 'fred', you know there is just one static class.
 
Upvote 0

Daestrum

Expert
Licensed User
Longtime User
@LucaMs no it's not the same as you set one object equal to the other.
B4X:
Private MySecondObj As MyClass = MyObj
 
Upvote 0

avacondios

Active Member
Licensed User
Longtime User
As I am understanding, it is not needing to execute the initialize sub, except if you want to pass parameters inside on the class.

Erel, am I correct ?
 
Upvote 0
Top