Constructors
A constructor is a special member function whose task is to initialize the objects of it's class. This is the first method that is run when an instance of a type is created. A constructor is invoked whenever an object of it's associated class is created. If a class contains a constructor, then an object created by that class will be initialized automatically. We pass data to the constructor by enclosing it in the parentheses following the class name when creating an object. Constructors can never return a value, and can be overridden to provide custom intitialization functionality. In Visual Basic we create constructors by adding a Sub procedure named New to a class. The following code demonstrates the use of constructors in Visual Basic.
Module Module1
Sub Main()
Dim con As New Constructor(10)
WriteLine(con.display())
'storing a value in the constructor by passing a value(10) and calling it with the
'display method
Read()
End Sub
End Module
Public Class Constructor
Public x As Integer
Public Sub New(ByVal value As Integer)
'constructor
x = value
'storing the value of x in constructor
End Sub
Public Function display() As Integer
Return x
'returning the stored value
End Function
End Class
Destructors
A destructor, also know as finalizer, is the last method run by a class. Within a destructor we can place code to clean up the object after it is used, which might include decrementing counters or releasing resources. We use Finalize method in Visual Basic for this and the Finalize method is called automatically when the .NET runtime determines that the object is no longer required. When working with destructors we need to use the overrides keyword with Finalize method as we will override the Finalize method built into the Object class. We normally use Finalize method to deallocate resources and inform other objects that the current object is going to be destroyed. Because of the nondeterministic nature of garbage collection, it is very hard to determine when a class's destructor will be called. The following code demonstrates the use of Finalize method.
Module Module1
Sub Main()
Dim obj As New Destructor()
End Sub
End Module
Public Class Destructor
Protected Overrides Sub Finalize()
Write("hello")
Read()
End Sub
End Class
When you run the above code, the word and object, obj of class, destructor is created and "Hello" is displayed. When you close the DOS window, obj is destroyed.
Popular Posts
-
<%@page contentType="text/html" import="java.util.*" %> <!-- http://www.roseindia.net/jsp --> ...
-
Syntax of JSP Scriptles are: <% //java codes %> JSP Scriptlets begins with <% and ends %> .We can embed any a...
-
JSP pages are high level extension of servlet and it enable the developers to embed java code in html pages. JSP files are finally compiled ...
-
package interfaceexercise; // Define an interface with three abstract methods. // Any class that implements this interface has to // impleme...
-
Syntax of JSP Declaratives are: <%! //java codes %> JSP Declaratives begins with with .We can embed any amount of java c...
-
In this section we are going to implement insert data, delete data, and update data using with JDBC database and also using of JavaScript. ...
-
Learning about Properties Every component (form is component for example) has properties, that determine its look and its functioning. Prope...
-
Steps to follow: 1. Write Relation.java as shown in Code-11.5.a below. Please note that the Relation is an interface. (You are welcome to d...
-
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE>Using JavaServer Pages...
-
Now I will show you how to retrieve the data posted from a HTML file in a JSP page. Consider an html page that prompts the user to enter his...
No comments:
Post a Comment