Computer Science Canada

Static

Author:  Martin [ 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.

Author:  wtd [ Fri Jul 30, 2004 6:18 pm ]
Post 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".

Author:  rizzix [ Fri Jul 30, 2004 6:48 pm ]
Post 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.

Author:  wtd [ Fri Jul 30, 2004 6:52 pm ]
Post 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.

Author:  Martin [ Fri Jul 30, 2004 7:01 pm ]
Post subject: 

I'm still lost.

Can you show me what you mean with code?

Author:  wtd [ Fri Jul 30, 2004 7:09 pm ]
Post 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):

code:
int a = 42
a.cos()


Instead you have to provide the closest thing Java can come to a non-object-oriented function:

code:
int a = 42
Math.cos(a)

Author:  Martin [ Fri Jul 30, 2004 7:14 pm ]
Post 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?

Author:  wtd [ Fri Jul 30, 2004 7:24 pm ]
Post 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());
                }
        }
}

Author:  Martin [ Fri Jul 30, 2004 8:17 pm ]
Post subject: 

Gotchya Wink

Thanks Very Happy


: