I am new to object oriented programming, and I was following a tutorial in which they mentioned that in java, the keyword static means that the particular member belongs to a type itself, rather than to an instance of that type. I have read this definition a lot of times across the internet but this is complex for me to understand, can anyone tell me in simple language what it means? Thanks!
java - What does the 'static' keyword do in a class? - Stack Overflow
In laymans terms, what does 'static' mean in Java? - Stack Overflow
programming languages - False friends? Keyword "static" in C compared to C++, C# and Java - Software Engineering Stack Exchange
What is the advantage of "companion object" vs static keyword - Language Design - Kotlin Discussions
Videos
static members belong to the class instead of a specific instance.
It means that only one instance of a static field exists[1] even if you create a million instances of the class or you don't create any. It will be shared by all instances.
Since static methods also do not belong to a specific instance, they can't refer to instance members. In the example given, main does not know which instance of the Hello class (and therefore which instance of the Clock class) it should refer to. static members can only refer to static members. Instance members can, of course access static members.
Side note: Of course, static members can access instance members through an object reference.
Example:
public class Example {
private static boolean staticField;
private boolean instanceField;
public static void main(String[] args) {
// a static method can access static fields
staticField = true;
// a static method can access instance fields through an object reference
Example instance = new Example();
instance.instanceField = true;
}
[1]: Depending on the runtime characteristics, it can be one per ClassLoader or AppDomain or thread, but that is beside the point.
It means that there is only one instance of "clock" in Hello, not one per each separate instance of the "Hello" class, or more-so, it means that there will be one commonly shared "clock" reference among all instances of the "Hello" class.
So if you were to do a "new Hello" anywhere in your code: A- in the first scenario (before the change, without using "static"), it would make a new clock every time a "new Hello" is called, but B- in the second scenario (after the change, using "static"), every "new Hello" instance would still share and use the initial and same "clock" reference first created.
Unless you needed "clock" somewhere outside of main, this would work just as well:
package hello;
public class Hello
{
public static void main(String args[])
{
Clock clock=new Clock();
clock.sayTime();
}
}
static means that the variable or method marked as such is available at the class level. In other words, you don't need to create an instance of the class to access it.
public class Foo {
public static void doStuff(){
// does stuff
}
}
So, instead of creating an instance of Foo and then calling doStuff like this:
Foo f = new Foo();
f.doStuff();
You just call the method directly against the class, like so:
Foo.doStuff();
In very laymen terms the class is a mold and the object is the copy made with that mold. Static belong to the mold and can be accessed directly without making any copies, hence the example above
I have a book about the design of C++ by Bjarne Stroustrup (the inventor of C++). I don't have it right here so I can't lookup the exact quote right now, but in it he admits that when he added static to C++, he didn't fully understand what it meant in C. So that's why in C++ it has a different meaning than in C.
Java and C# inherited the meaning from C++.
Notably, C++ has both uses of static, and I think there's a third somewhere.
Generally, I think that the C use of static does not correspond at all to any English usage of the word, whereas I think that static as a static member variable, for example, makes a lot more sense.
Remember that as a language designer, you have a fair incentive to introduce fewer keywords into the language, to disallow less code- especially when you're trying to be source-compatible with C, as in C++, and the static keyword already existed, so they couldn't break any C programs trying to compile as C++ by re-using it.
Edit: I knew there was a third. C has function-level static variables. The static member is just a scoped version of this functionality. Therefore, both uses of static originate from C.