2

I can't seem to understand the static key word (java) so I googled it up and viewed a thread in this website, though I'm sure the answer was conclusive and clear -it always is over here- I didn't seem to understand it for two reasons; I'm not a native English speaker and the language was a bit vague for me, and it lacked exemples of use in classes, instance of classes, interfaces (if possible), instance of interfaces and variables, lists and arrays ect. I would really appreciate any help and please keep the English as simple as possible ;) Thank you Aditsan

Note from editor: Please note that the original poster is asking for examples, and is not a native English speaker as you provide answers. From the comments, it appears that OP doesn't understand the concept well enough to ask about the parts that don't make sense yet, so examples would be awesome! It may take extra details and multiple different explanations to find the combination of answers that works best.

Community
  • 1
  • 1
Aditsan Kadmus
  • 106
  • 1
  • 1
  • 10
  • 6
    Can you link the answer that you are talking about, which didn't make you understand, so that people here don't repeat the same thing again. – Rohit Jain Jan 30 '13 at 21:47
  • And if possible, ask about the specific things which you didn't understand. – millimoose Jan 30 '13 at 21:47
  • It means there will be only one version of it per-class. Where "it" is either a method or a variable. (obviously there a few more intricacies, but that is it in a nutshell). – CasualT Jan 30 '13 at 21:47
  • @CasualT.. There is more to `static` keyword, than just having one version, which after all does not differentiate a static method with instance one. – Rohit Jain Jan 30 '13 at 21:48
  • yes. but I wanted to keep it as simple as possible to get him started and to distinguish it from the variables that are 'normal'/not-static and that get new versions per object instantiated. – CasualT Jan 30 '13 at 21:49
  • 1
    Thank you guys for the fast responses Here is the topic as requested http://stackoverflow.com/questions/413898/what-does-the-static-keyword-do-in-java Thank you again but I want to learn more about the static keyword and if it does more than saying there will be only one version of this then what does it do because if the cause of using it is saying there will be only one version then why not use final (so we make it unchangeable from the very beginning)? – Aditsan Kadmus Jan 30 '13 at 21:57

2 Answers2

4

I think it helps to understand what non-static means, i.e. field/methods/... that are declared without the keyword static.

Every field declared without the keyword static exists as part of an object. If you have two objects, each of these two objects has a field with possibly different contents:

class X {
 int f;
}

X x1 = new X();
X x2 = new X();
x1.f = 5;
x2.f = 10;
// x1.f still is 5

However, static fields exist not per object, but per class. So in the following example, there is only one field g no matter how many (if any!) objects of class Y you have:

class Y {
  static int g;
}

Y y1 = new Y();
Y y2 = new Y();
y1.g = 5;
y2.g = 10;
// y1.g is 10, because y1.g and y2.g mean the exact same thing

I personally think accesses to static fields should be made using the class (Y.g) instead of mentioning object instances (y1.g), so that the existence without any object instance is more explicit.

For methods the difference is that non-static methods are associated to an object instance, which can be accesses using this inside the method. When invoking a method declared with void m() you can access non-static (and static) fields of the object it is invoked on (so for x1.m() from the example above you can get to the field containing 5, for x2.m() you can access the field containing 10.

Static methods, however, can be invoked without having a (corresponding?) object around. If the declaration is static void n() inside class Y, you can call this method using Y.n() or y1.n() (if y1 is an instanceof Y, as above). Here, too, I prefer the first way of writing it down. Because in static methods you do not have a reference to the object instance (which is named this in non-static methods), you cannot access specific non-static fields from inside a static method - simply because there is no clear association to a specific object.

Regarding static and class definitions: This is rather advanced. You can declare a class inside another class. If the inner class is not static, every object instance of the inner class also has a reference to an instance of the outer class (which also means that you only can create an instance of the inner class if you have an instance of the outer class). This is not always what you want. By declaring the inner class static it just exists and can be used, more or less, like a class defined in its own file.

C-Otto
  • 5,615
  • 3
  • 29
  • 62
  • Thanks for that answer it cleared up a lot of things for me but I still have some doubts when it comes to methods.. Also if we have an entire class that is static does that makes all of its component static? – Aditsan Kadmus Jan 30 '13 at 22:26
  • Regarding methods: What is it you don't understand? You can think of any non-static method as a static method where the very first argument is named `this` and _always_ points to an instance of the class the method is declared in (so the method _cannot_ be called without providing such an instance). Regarding classes: no. The static/non-static property of an inner class just defines whether it is possible to access _the_ instance of the outer class. – C-Otto Jan 30 '13 at 22:28
  • ok this is more than close enough for me :) Thanks a lot for the effort and for providing a simply composed well structured answer (with examples witch is a huge benefit) one last thing though, is there static interfaces ? And if there are at least their methods can't be static right? {Thank you again} – Aditsan Kadmus Jan 30 '13 at 22:35
  • This question is rather advanced and during programming, I think, this would not pop up normally. However for completeness, I suggest reading this Q/A: http://stackoverflow.com/questions/71625/why-would-a-static-inner-interface-be-used-in-java – C-Otto Jan 30 '13 at 22:38
  • Good, thanks I'll keep reading about it I can't believe I've skipped this concept learning Java!!! Any way thanks I sure now know why the main method should be static :) – Aditsan Kadmus Jan 30 '13 at 22:42
  • Great! In case another reader wonders why `main` is `static`: If not, where would the object instance for `this` come from? When you just started your program, no object instance was created, yet. – C-Otto Jan 30 '13 at 22:43
0

Basically, static implies/provides two things:

1) Only one instance of an "item" exists in the whole system (JVM) 2) Static "items" are also context/state free

To explain (1) above: Suppose you have a Meal Token issuer. No matter how many users/processes are there in the system, all tokens must be issued by a single "thing". You would develop that "thing" as static. You would then decide what that "thing" is. It could be a class that does a complex operation and implements a complex business rule. Then you would have a single static class issuing tokens in "a single uniform way" for the whole system. Some times, all that matters is that the token should be "static" but how it is issued could be non-static. Then you would simply implement a "Static" token counter.

To explain (2) : Going by what is said for (1) above, you can easily see why it is important that the static "things" operate in a context-free manner. That is, they do not know who calls them or for what purpose. When they are called, they do not borrow anything from the past, they need all inputs from the current caller, and they just do their job, and remember nothing for the future.

srini.venigalla
  • 5,137
  • 1
  • 18
  • 29
  • I don't think this a good explanation of static, sorry, rather of one use case such as Singletons. – Puce Jan 30 '13 at 22:00
  • So what's the difference between static and final other that final can have many instances? – Aditsan Kadmus Jan 30 '13 at 22:02
  • @puce Every static variable is a singleton. – srini.venigalla Jan 30 '13 at 22:03
  • @Adistan final is immutable, that is, once initialized, its value cannot change. You can have static finals or static non-finals – srini.venigalla Jan 30 '13 at 22:04
  • ok thanks I'm seeing all the different answers and trying to fully understand it and I'm close to that I think ... Fingers are crossed !! :) – Aditsan Kadmus Jan 30 '13 at 22:23
  • @srini.venigalla You can have (possibly several) static and non-static instances of the same class at the same time. You can also use the static keyword with methods and classes. – Puce Jan 30 '13 at 22:24
  • @srini.venigalla That's not quite what a singleton means. A singleton is an object of which only one instance can ever exist. A variable isn't really an object definition, and a `static` field can have many different values assigned to it. The concepts just aren't related. – millimoose Jan 31 '13 at 00:01