What does the 'static' keyword do in a class?
Solution 1
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.
Solution 2
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();
}
}
Solution 3
The static
keyword means that something (a field, method or nested class) is related to the type rather than any particular instance of the type. So for example, one calls Math.sin(...)
without any instance of the Math
class, and indeed you can't create an instance of the Math
class.
For more information, see the relevant bit of Oracle's Java Tutorial.
Sidenote
Java unfortunately allows you to access static members as if they were instance members, e.g.
// Bad code!
Thread.currentThread().sleep(5000);
someOtherThread.sleep(5000);
That makes it look as if sleep
is an instance method, but it's actually a static method - it always makes the current thread sleep. It's better practice to make this clear in the calling code:
// Clearer
Thread.sleep(5000);
Solution 4
The static
keyword in Java means that the variable or function is shared between all instances of that class as it belongs to the type, not the actual objects themselves.
So if you have a variable: private static int i = 0;
and you increment it (i++
) in one instance, the change will be reflected in all instances. i
will now be 1 in all instances.
Static methods can be used without instantiating an object.
Solution 5
Basic usage of static members...
public class Hello
{
// value / method
public static String staticValue;
public String nonStaticValue;
}
class A
{
Hello hello = new Hello();
hello.staticValue = "abc";
hello.nonStaticValue = "xyz";
}
class B
{
Hello hello2 = new Hello(); // here staticValue = "abc"
hello2.staticValue; // will have value of "abc"
hello2.nonStaticValue; // will have value of null
}
That's how you can have values shared in all class members without sending class instance Hello to other class. And whit static you don't need to create class instance.
Hello hello = new Hello();
hello.staticValue = "abc";
You can just call static values or methods by class name:
Hello.staticValue = "abc";
Timmy Von Heiss
Updated on July 08, 2022Comments
-
Timmy Von Heiss almost 2 years
If we have:
#app/assets/javascripts/tabbed_panels.js var new_items = function() { #do something }); $(document).on("turbolinks:load", new_items);
and
#app/assets/javascripts/paginate_users.js var new_users = function() { #do something }); $(document).on("turbolinks:load", new_users);
etc
Is there a way to avoid repeating
$(document).on("turbolinks:load", '#');
for every script? -
Timmy Von Heiss over 7 yearsThanks. Do you know if there is a way I can do something like this with a ruby method so I can dry up all the
data: {turbolinks: false}
in my<%= link_to ... %>
stackoverflow.com/questions/41336325/… -
coreyward over 7 years@TimmyVonHeiss Ideally you're not doing much/any of that, but you can always either override
link_to
or create a separate helper that wrapslink_to
or itsoptions
argument (e.g.link_to foo, foo_path(@foo), default_link_options
where you've defineddefault_link_options
elsewhere). -
Timmy Von Heiss over 7 yearsIt seems like very few of my jquery scripts work correctly when the page is opened through a
link_to
unless I havedata: {turbolinks: false}
-
coreyward over 7 years@TimmyVonHeiss Are your JS include tags in the body? If so, that's probably what's causing issues for you.
-
Timmy Von Heiss over 7 yearsMy JS include tags are in the head:
<%= javascript_include_tag 'application', 'data-turbolinks-track' => true %>
-
Timmy Von Heiss over 7 yearsfwiw, i figured out that what i thought were turbolinks problems was really problems related to a scroll script that paginates new pages that i wrote.. it was carrying over to other pages. i fixed it. it was not related to turbolinks.