|
-
Oct 31st, 2002, 02:24 PM
#1
Static keyword...what does it mean...
I'm not a Java programmer. I use VB, but I have to do an assignment in Java. And I have problems about when to use static, and when not to use it. I really don't understand what it means. So can you please use all your forces to try to explain what it means? What is a Static variable, and what is a static method and so on....
Pleas explain like if I was 5years old or something like that...
-
Oct 31st, 2002, 04:50 PM
#2
Hyperactive Member
A static variable contains the same value across all instances of a class. For an example, if you have a class, call it myClass, which contains a non static variable myVar and a static variable statVar. Now if you have two instances of the class, ie
myClass var1 = new myClass();
myClass var2 = new myClass();
two different portions of memory are reserved for myVar, but only one for statVar. In other words, whether you reference statVar from var1 or var2, they will both return the same result, no matter what you have changed in them. On the other hand, if you reference myVar, it will contain the data that the specific class put in it.
A static procedure is a procedure that doesn't change any non static variables. It is a procedure that can be called without instantiating the class first. The entire Math class consists of static procedures. I think they call such classes utility classes.
Hope that helps
-
Oct 31st, 2002, 04:59 PM
#3
Thanks that was very informing. It was really easy to understand what you meant, even for me who aint that god in English.
Thanks again.
-
Nov 4th, 2002, 01:15 PM
#4
Does Java have static local variables? It would be another meaning of the static keyword.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 4th, 2002, 02:12 PM
#5
I'm not sure....does anyone else know...what do you exactly mean my local variable....that it is in a procedure or something like that????
-
Nov 4th, 2002, 03:59 PM
#6
Hyperactive Member
No. Local variables are only static if the procedure is static. You cannot declare a static variable inside a procedure.
-
Nov 4th, 2002, 04:57 PM
#7
What I mean is this C function:
Code:
void somefunc(void)
{
static int calls = 0;
printf("Call No. %i\n", ++calls);
}
Is this possible in Java?
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 5th, 2002, 02:14 PM
#8
Hyperactive Member
No, as I said, you cannot use the static keyword inside a procedure. To make a static variable the entire procedure must be static:
Code:
private static void somefunc()
{
int calls = 0; //this var is static
}
private void somefunc()
{
static int callls = 0; //this is a syntax error
}
-
Nov 5th, 2002, 03:04 PM
#9
private static void somefunc()
{
int calls = 0; //this var is static
}
That doesn't make sense. static member functions and static local variables use a completly different meaning of static, so it would be a HUGE language flaw if it were so.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 5th, 2002, 10:40 PM
#10
Originally posted by marnitzg
Code:
private static void somefunc()
{
int calls = 0; //this var is static
}
I don't see how this could be. Since you must initialize a variable before using it, wouldn't the value be getting reset every time the method is called anyway?
I thought only global variables could be static.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 6th, 2002, 01:08 PM
#11
Hyperactive Member
Hmm, think you're right. Reasons:
1. 2 threads running the same procedure
2. Recurring functions would lose information
Therefore I think you're right, only global variables can be static
-
Nov 6th, 2002, 01:10 PM
#12
Hyperactive Member
Originally posted by crptcblade
I don't see how this could be. Since you must initialize a variable before using it, wouldn't the value be getting reset every time the method is called anyway?
So according to this logic, a static global variable would also be useless.
-
Nov 6th, 2002, 01:23 PM
#13
Originally posted by marnitzg
So according to this logic, a static global variable would also be useless.
Well, no, I may have stated it wrong. The life time of a global variable is the life time of the class, and the life time of a static global variable is the life time of the calling program.
On the other hand, a local variable has a life time of the procedure only, in Java anyway. So when a global variable is initialized, it is still initialized the next time it is accessed. But a local variable would need to be reallocated, and therefore reinitialized everytime you call the procedure it is in.
Laugh, and the world laughs with you. Cry, and you just water down your vodka.
Take credit, not responsibility
-
Nov 6th, 2002, 03:50 PM
#14
One of the most confusing things about C++ is that the static keyword has 3 different meanings:
Meaning a:
Code:
static int someGlobalVar;
static void someGlobalFunc;
static here means that the variable/function is local to the code module. It is not exported by the linker and cannot be referenced from other modules. This avoids ambiguity errors.
Meaning b:
Code:
void someFunc(void)
{
static int statVar;
}
This variable is static to the function. It's the meaning that most accuratly hits the meaning of the word 'static'. It doesn't lose it's value between calls to the function. It's like a global variable that can only be accessed from within the function it's declared in.
Meaning c (new to C++):
Code:
class A {
public:
static int s_iVar;
static void Func(void);
};
static here means that the variable/function belongs to the class instead to it's instances. This is like in Java. Can be accessed without an instance (in C++ you use the scope resolution operator for this: A::s_iVar = 3; A::Func();
Meaning b is useless in Java, but what about meaning a? It is very useful, and I wouldn't like having to define class variables for a thing I only want accessible from a single function.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|