Quote:
Originally Posted by 2185Bilal
Wow..thank you very much. And what was the thing you told me about the "final"
ps. I pretty new to Java,  and i think "final" is like a variable type (string, integer), so could you please tell what is a final.
Thanks
|
"final" is a modifier to a variable or Object (variables being integers, doubles; Objects being Strings, Jaguars, Joysticks, etc). If you put "final" in front of a variable/Object, that means that it cannot be changed by any method in any class of your code. Basically, "final" means "constant."
For example:
Code:
Jaguar jaguar = new Jaguar(1);
jagaur = new Jaguar(2);
void changeJag(){
jaguar = new Jaguar(3);
}
A non-final object can be reassigned values, like I showed above. The Jaguar is not "final," so you can change it's value anywhere in your code.
Code:
final Jaguar jaguar = new Jaguar(1);
jaguar = new Jaguar(2);
void changeJag(){
jaguar = new Jaguar(3);
}
The above code will throw an error while compiling because the Jaguar object is declared "final." If you declare an object "final," it cannot be assigned a new value. Declaring your objects "final" means that you do not want them to have values other than the ones you give them. For example, if you have a Jaguar controlling your front-left wheel, you do not want to give it a new value in the code so that your robot thinks that your front-left wheel is the rear-left wheel.