Instance Variables
For my fellow students/programmers (and tutees):
Instance variables are variables that are declared at the top of a class. They are created any time an object of that class is instantiated. Hence, their name.
For example:
In my previous blog entry I talked about constructors and my examples there made use of an Employee Class and the instance variables name and id. I'm going to show an example of an employee class that has both an empty constructor, a constructor with parameters, and instance variables. So let's see what a simple Employee class would look like for VB .Net and for Java. (By the way, an Employee class is an excellent example of a type of class that would go into a Class Library of an application)
**** VB .Net ****
Public Class Employee
Private name As String
Private id As String
Public Sub New()
End Sub
Public Sub New(ByVal newName As String, ByVal newId As String)
name = newName
id = newId
End Sub
End Class
****
**** Java ****
public class Employee {
private String name = null;
private String id = null;
public Employee() {
}
public Employee(String newName, String newId) {
name = newName;
id = newId;
}
}
****
Next time: VB .Net Properties and Java getters & setters
Instance variables are variables that are declared at the top of a class. They are created any time an object of that class is instantiated. Hence, their name.
For example:
In my previous blog entry I talked about constructors and my examples there made use of an Employee Class and the instance variables name and id. I'm going to show an example of an employee class that has both an empty constructor, a constructor with parameters, and instance variables. So let's see what a simple Employee class would look like for VB .Net and for Java. (By the way, an Employee class is an excellent example of a type of class that would go into a Class Library of an application)
**** VB .Net ****
Public Class Employee
Private name As String
Private id As String
Public Sub New()
End Sub
Public Sub New(ByVal newName As String, ByVal newId As String)
name = newName
id = newId
End Sub
End Class
****
**** Java ****
public class Employee {
private String name = null;
private String id = null;
public Employee() {
}
public Employee(String newName, String newId) {
name = newName;
id = newId;
}
}
****
Next time: VB .Net Properties and Java getters & setters

2 Comments:
Make a lot of information, more than 5000 dollars a week making please visit my blog
Hi, Nice stuff. I found a cool news widget for our blogs at www.widgetmate.com. Now I can show the latest news on my blog. Worked like a breeze.
Post a Comment
<< Home