Class Variables vs Instance Variables
66This is a post somewhat superficial only to take some doubts that sometimes people have between these two types of variables.
Under the object oriented paradigm, classes that we created may have variables with several states (protected, public and private) and two different types: class variables and instance variables.
Instance Variables
They are those variables that for each new instance of class (every new object that is created) they start with different values.
Java sample:
public class Boat
{// Instance variables
private String registration;
private String flag;
private String captainName;
private float lenght;
private float tonnage;
}
All these variables, for each new boat that is created will have different values, so it is justified to be instance variables.
Class Variables
They are those variables that will have a fixed value for all instances of that class. When you change a value in a class all instances of that class will have that value changed.
Java sample:
public class Boat
{// Class variables
private static float boatFee;
// Instance variables
private String registration;
private String flag;
private String captainName;
private float lenght;
private float tonnage;
}
As the rate is a fixed amount, it is fair to become a variable of class so all instances of Boat created will have the same rate applied to the boat.
This post is on my personal blog too, in portuguese languange, here.
|
|
Understanding Object-Oriented Programming With Java: Up
Current Bid: $10.42
|
|
|
An Object-Oriented Approach to Programming Logic
Current Bid: $6.00
|
| No Photo |
Object-Oriented Programming for Dummies, Namir Clement
Current Bid: $8.49
|
|
|
Object-Oriented Programming with Visual Basic .NET, J.P
Current Bid: $16.38
|
|
|
Object-Oriented Programming in C++/Book and Disk (The W
Current Bid: $11.57
|
|
|
Object-Oriented Programming with ActionScript 2.0 (VOIC
Current Bid: $4.98
|
PrintShare it! — Rate it: up down [flag this hub]







