
-----------------------------------
Martin
Fri Jul 30, 2004 6:12 pm

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.

-----------------------------------
wtd
Fri Jul 30, 2004 6:18 pm


-----------------------------------
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
Fri Jul 30, 2004 6:48 pm


-----------------------------------
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
Fri Jul 30, 2004 6:52 pm


-----------------------------------
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
Fri Jul 30, 2004 7:01 pm


-----------------------------------
I'm still lost.

Can you show me what you mean with code?

-----------------------------------
wtd
Fri Jul 30, 2004 7:09 pm


-----------------------------------
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):

int a = 42
a.cos()

Instead you have to provide the closest thing Java can come to a non-object-oriented function:

int a = 42
Math.cos(a)

-----------------------------------
Martin
Fri Jul 30, 2004 7:14 pm


-----------------------------------
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
Fri Jul 30, 2004 7:24 pm


-----------------------------------


Correct.



It isn't associated with any instance of a class.

and b) What would be a not static way to do that?

If Integer could be subclassed...

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());
		}
	}
}

-----------------------------------
Martin
Fri Jul 30, 2004 8:17 pm


-----------------------------------
Gotchya ;)

Thanks :D
