- Overview of an object
- Syntax for declaring/instantiating an object
- Declaring/Instantiating an object
- Declaring/Instantiating a Null object
- Remark: Current object
- Members and methods
- Lifespan of an object
- Reference on a local object
- Returning an object
- Objects in the loops
- Freeing the object
Instantiation of an object
To access a class, the object
must be declared as being part of the class to handle, this is called object instantiation
An instance is an object that belongs to a given class.
To handle an object, you must:
- Describe the class.
- Instantiate the object.
An object can be passed in parameter to a function or to a procedure.
: You have the ability to dynamically instantiate an object to a class. The implementation of polymorphism requires the dynamic instantiation. For more details, see Dynamic instantiation
Example of object: "SourceFile" is object of "File" class. For this object, you have the ability to handle the members named "Name", "Extension", etc.
SourceFile is File object
SourceFile is File
Syntax for declaring/instantiating an object
Declaring/Instantiating an object
<ObjectName> is [object] <Class Name> ([<Parameters>])
Details of syntax
Name that identifies the instance of the class.
Name that identifies the class, defined when creating the class in the code editor.
Declaring/Instantiating a Null object
<ObjectName> is [object] <Class Name> ([<Parameters>]) = NULL
This syntax is used to declare an object without instantiating it. In this case:
- the <- operator is used to take a reference on the object.
- the declared variable behaves as if the declaration was a non-dynamic declaration: the = operator performs a copy of the members.
- the object handled can be returned as return value of the procedure or method.
- in some advanced cases, the object handled can be destroyed to anticipate the automatic destroy operation.
Remark: Current object
The Object keyword can also be used to identify the current object.
You can also use the This keyword to access the current object.
Reference on a local object
The object is created during its declaration. The object is local by default.
The local objects are not automatically destroyed at the end of the process execution. Only the local reference is freed.
Therefore, if another reference was taken on the object, the object will not be destroyed when the local reference is freed.
// Declaration code of global variables
gpo is Class1 dynamic
gArr is array of Class1 dynamic
// Any code
o1, o2, o3 are Class1
gpo1 = o1
// At the end of process
// o1 is not destroyed because it is referenced by the global variable gpo
// o2 is not destroyed because it was added to the gArr array
// o3 is not destroyed because there is only the local reference
Returning an object
You have the ability to return an object handled by a dynamic or non-dynamic local variable.
Remark: In the earlier versions, you only had the ability to return an object handled by a dynamic local variable.
Objects in the loops
In a loop, the objects can be handled by non-dynamic local variables and without explicit allocation.
The objects declared in a loop are reallocated at each iteration of the loop and the reference on the object of the previous iteration is freed:
- if no other reference was taken, the object is destroyed.
- if a reference was taken (global variable, addition to an array, and so on), the object is not destroyed.
// build the array
t is array of Class1 dynamic
FOR I = 1 TO 10
// create an object corresponding to the subscript
o is Class (x)
o.m_nIndex = i
// add the object into the array
Freeing the object
The objects used by non-dynamic local variables can be explicitly destroyed by the DELETE
keyword to anticipate the automatic destruction.
Remark: This syntax is an advanced syntax and it should be used in specific cases only. In most cases, the destroy operation is automatic.
Reminder: In most cases, the use of the DELETE keyword is not required. The WLanguage automatically destroys the objects that are no longer used by any variable. The most common cases are:
- destroying a set of objects that are referenced in a cyclical way
- freeing the resources used by the instance (sockets, files, ...) in advance via the execution of the destructor.
Special case in Java/Android
The concept of destructor does not exist in Java/Android. Therefore, the code found in the destructor of a class will never be run.
An object is freed by the garbage collector when it is no longer referenced in the application. There is no way to foresee the moment when this operation occurs.
To force an object to be freed, you have the ability to assign it to NULL or to use the Free
This page is also available for…
Click [Add] to post a comment