Author |
Message |
Martin
|
Posted: Fri Jul 30, 2004 6:12 pm Post subject: Static |
|
|
I'm learning Java right now, and 'static' is confusing the hell out of me.
My understanding of it is that a non-static object (dynamic?) has unique variables for every instance, wheras a static instance shares the variable's values for every instance.
If I'm wrong, please correct me. I can't picture when you'd ever want to use a static object, save for public static void main. |
|
|
|
|
|
Sponsor Sponsor
|
|
|
wtd
|
Posted: Fri Jul 30, 2004 6:18 pm Post subject: (No subject) |
|
|
Static variables and methods (at least in Java, as other languages may have other definitions of "static") can be thought of as "class variables".
They aren't associated with any particular instance of a class.
There's nothing particularly more "dynamic" about variables not marked as "static". |
|
|
|
|
|
rizzix
|
Posted: Fri Jul 30, 2004 6:48 pm Post subject: (No subject) |
|
|
it is dynamic only cuz space is not allocated for those variables only until u declare space to be allocated using the "new" keyword
for class variables on the other hand space is allocated the moment ur program runs. |
|
|
|
|
|
wtd
|
Posted: Fri Jul 30, 2004 6:52 pm Post subject: (No subject) |
|
|
Yep. Even then, it's more memory-efficient to only create variables once, rather than once for every instance of the class, assuming that doing so doesn't break the program, of course. |
|
|
|
|
|
Martin
|
Posted: Fri Jul 30, 2004 7:01 pm Post subject: (No subject) |
|
|
I'm still lost.
Can you show me what you mean with code? |
|
|
|
|
|
wtd
|
Posted: Fri Jul 30, 2004 7:09 pm Post subject: (No subject) |
|
|
Consider, for example, the Math class and the static members it provides.
Since ints are not objects (unfortunately), you can't do something like (psuedocode):
Instead you have to provide the closest thing Java can come to a non-object-oriented function:
code: | int a = 42
Math.cos(a) |
|
|
|
|
|
|
Martin
|
Posted: Fri Jul 30, 2004 7:14 pm Post subject: (No subject) |
|
|
More like
i = Math.cos(a);
right?
and that is static, correct?
a) What makes it static
and b) What would be a not static way to dothat? |
|
|
|
|
|
wtd
|
Posted: Fri Jul 30, 2004 7:24 pm Post subject: (No subject) |
|
|
[quote="Darkness"]More like
i = Math.cos(a);
right?
and that is static, correct?Quote:
Correct.
[quote="Darkness"]a) What makes it static
It isn't associated with any instance of a class.
Darkness wrote: and b) What would be a not static way to do that?
If Integer could be subclassed...
code: | import java.lang.*;
import java.io.*;
public class OOMath {
public static void main(String[] args) {
MyInt i = new MyInt(1);
System.out.println(i);
}
public static class MyInt extends Integer {
public MyInt(int value) {
super(value);
}
public MyInt(String s) {
super(s);
}
public double cos() {
return Math.cos(doubleValue());
}
}
} |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Martin
|
Posted: Fri Jul 30, 2004 8:17 pm Post subject: (No subject) |
|
|
Gotchya
Thanks |
|
|
|
|
|
|