Wish Initialize with no parameters

Daestrum

Expert
Licensed User
Longtime User
May sound like an odd wish, but why do we have to call initialize on objects that take no parameters?

for example

B4X:
Dim sb as StringBuilder
....
sb.Initialize
...

Surely the fact we have called Dim/Private etc should be sufficient to have the object initialized for us.

Obviously, for objects that take parameter(s) it makes sense to have to call the initialize routine.

So my wish is, on objects that take no parameters on their initialize, can that be done when they are defined. (just like int does).
 

Roycefer

Well-Known Member
Licensed User
Longtime User
Isn't the purpose of Initialize to avoid the unnecessary creation of an Object when all we wanted is a reference?

Currently:
B4X:
Dim sb As StringBuilder   'creates a reference to a StringBuilder Object
'StringBuilder sb;

sb.Initialize  'creates a new, empty StringBuilder Object and then points sb to it
'sb = new StringBuilder();

'We can also do this:
Dim sb As StringBuilder = PreexistingStringBuilder
'StringBuilder sb;
'sb = PreexistingStringBuilder;
'No need to create that new empty StringBuilder

With automatic Initialization:
B4X:
Dim sb As StringBuilder   'creates a reference to a StringBuilder Object AND creates a new empty Object and then points the reference to it
'StringBuilder sb = new StringBuilder();

'But if we wanted to do this:
Dim sb As StringBuilder = PreexistingStringBuilder
'StringBuilder sb = new StringBuilder();   <--Unnecessary, wasted Object creation
'sb = PreexistingStringBuilder;
 

jmon

Well-Known Member
Licensed User
Longtime User
In some libraries, the intialize method is called to initialize some variables with default values, or to initialize the lists & maps. I guess that's why some don't have any parameters
 

Daestrum

Expert
Licensed User
Longtime User
Dim sb As StringBuilder 'creates a reference to a StringBuilder Object AND creates a new empty Object and then points the reference to it
'StringBuilder sb = new StringBuilder();


'But if we wanted to do this:
Dim sb As StringBuilder = PreexistingStringBuilder

Although I understand what you were trying to explain, this is exactly what happens with an Int
B4X:
Dim anInt as Int  'valid needs no initialize
Dim anotherInt As Int = yetAnotherInt
 

Roycefer

Well-Known Member
Licensed User
Longtime User
B4J Ints are translated into Java ints (a primitive, not an Object). Primitives, in Java, aren't references like Objects are. When a numerical primitive is automatically initialized to 0 (the JVM does this for us for global variables but Erel also does this for us when translating from B4J to Java), no extra int is created. It's just that the value of this new int is set to 0. That means it's not wasteful of memory and computation to do automatic initialization for primitives.

Also, primitives are stored in the stack which means their memory is automatically reclaimed when their owning function returns and their stack frame is discarded. Objects, on the other hand, are stored on the heap and their memory is reclaimed only when the Garbage Collector reclaims it. Unnecessarily creating Objects with automatic initialization would add a lot more workload to the GC.
 

Daestrum

Expert
Licensed User
Longtime User
As I said it was only a wish.
It's not worth arguing over when you look at the actual code generated for example by
B4X:
dim sb as stringbuilder = anotherstringbuilder
I can live with initialize with no parameters :)
 
Top