> Final variables are set when the app is launching
> regardless of where they are. Static variables are
> variables that will remain constant through all
> instances of that class.
This is totally wrong.
final variables can only be set once.
static variables are not "constant" and they operate on the class not instances.
> Final variables are set when the app is launching
> regardless of where they are.
No. They are set when the code that set them is encountered. Not before.
> Static variables are
> variables that will remain constant through all
> instances of that class.
No. Static variables can change their values as often as you like. Static means "associated with the class as a whole, rather than with any particular instance of the class." It has nothing to do with being constant.
> > Final variables are set when the app is launching
> > regardless of where they are. Static variables
> are
> > variables that will remain constant through all
> > instances of that class.
>
> This is totally wrong.
>
> final variables can only be set once.
>
> static variables are not "constant" and they operate
> on the class not instances.
This is not totally wrong, by being set when the application launches they can not be changed. With statics if you were to have something like this:
public class Main {
/** Creates a new instance of Main */
public Main() {
Fu a = new Fu();
a.add();
a.print();
Fu b = new Fu();
b.print();
b.add();
b.add();
a.print();
b.print();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
new Main();
}
}
public class Fu {
private static int var = 0;
/** Creates a new instance of Fu */
public Fu() {
}
public void add() {
var++;
}
public void print() {
System.out.println(var);
}
}
This would print:
1
1
3
3
Showing that static is consistant across instances of the class.
> > > Final variables are set when the app is
> launching
> > > regardless of where they are. Static variables
> > are
> > > variables that will remain constant through all
> > > instances of that class.
> >
> > This is totally wrong.
> >
> > final variables can only be set once.
> >
> > static variables are not "constant" and they
> operate
> > on the class not instances.
>
> This is not totally wrong,
No. You were TOTALLY WRONG. As in completely. As in 100% wrong.
Understand?
If you are confused then start a new thread but please don't force your wrong ideas onto others in their threads.
> This is not totally wrong, by being set when the
> application launches they can not be changed.
You are wrong.
Final variables are not set when the app launches. They're set when the code that sets them is encountered. This is easy to prove with code, but I'll leave it as an exercise for you.
> Showing that static is consistant across instances of
> the class.
If you mean that static is associated with the class rather than with any instance, then yes, you're correct. If you mean anything else, you're mistaken.
I was always under the impression that Final variables were set when the app launches setting them into stone so you couldn't change them at runtime. With the statics, I am just not great at wording things.
cotton.m, if someone is wrong about something, maybe you should explain why they are wrong instead of just saying wrong, you don't answer anyone's questions when you just say, "YOU'RE WRONG."
> otton.m, if someone is wrong about something, maybe
> you should explain why they are wrong instead of just
> saying wrong, you don't answer anyone's questions
> when you just say, "YOU'RE WRONG."
If you have questions then start YOUR OWN THREAD. Do not hijack someone else's thread. That is rude.
You are correct in that final variables cannot be changed but the below code is legal.
class Foo {
final int value; // has no value
Foo() {
value = 10; // when does this happen? Not when the app launches for sure.
}
}