Describe Member Variables vs. Local Variables ?
class Car {
String licensePlate = ""; // member variable
double speed; = 0.0; // member variable
double maxSpeed; = 123.45; // member variable
boolean isSpeeding() {
double excess; // local variable
excess = this.maxSpeed - this.speed;
if (excess < 0) return true;
else return false;
}
}
Until now all the programs you've look quite simple in structure. Every had exactly one class. This class had a single method, main(), that golds all the program logic and variables. The variables within those classes were all local to the main() method. They could not be accessed by anything outside the main() method. These are called as local variables.
This sort of program is the amoeba of Java. Everything the program needs to live is contained inside a single cell. It's quite an well-organized arrangement for little organisms, other than it breaks down while you need to design something bigger or more complex.
The licensePlate, speed and maxSpeed variables of the Car class, thus, belong to a Car object, not to any individual method. They are elaborates outside of any methods but inside the class and are used in different techniques. They are known as member variables or fields.
Member variable, instance variable, and field are different words in that mean the similar thing. Field is the preferred term in Java. Member variable is the preferred term in C++.
A member is not the similar as a member variable or field. Members included both fields and methods.