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.
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:
However, static fields exist not per object, but per class. So in the following example, there is only one field
gno matter how many (if any!) objects of classYyou have: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
thisinside the method. When invoking a method declared withvoid m()you can access non-static (and static) fields of the object it is invoked on (so forx1.m()from the example above you can get to the field containing 5, forx2.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 classY, you can call this method usingY.n()ory1.n()(ify1is an instanceofY, 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 namedthisin 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
staticand 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 classstaticit just exists and can be used, more or less, like a class defined in its own file.