In laymans terms, what does 'static' mean in Java?
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();
What does the 'static' keyword do in a class?
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.
Why does static have different meanings depending on the context?
Your examples are all correct, however, they all share a common feature. The word static means that an enclosing instance is not necessary.
Only a static inner class can exist without an enclosing instance. For example, if you have a class
Foo
and a non-static inner classBar
then you cannot create an instance ofBar
outside an instance ofFoo
.A static method means you do not need an instance of the class to call the method. You can call
String.format
without an actualString
instance for example.A static field will exist even without an instance of the class. If your
Foo
class has acounter
field that is static you can access it without ever instantiating an instance of theFoo
class.
Consider, as a clarifying point, that an interface can have static classes, static fields, and static methods. However, it cannot have the non-static version of any of those things (ignoring default methods which are sort of ad-hoc'd into the concept). This is because you can never create an instance of an interface so there could never be an enclosing instance.
You can also declare inner interfaces, annotations, and enums to be static although the keyword in that case is entirely redundant (e.g. similar to declaring an interface method abstract). Interfaces, annotations, and enums have no relationship to an enclosing class to begin with so static can't really take that away.
One last byzantine point. If you do a static import (import static pack.age.Foo.*
) you will be able to make unqualified references to any static items in a class (including interfaces, annotations, and enums regardless of whether or not they are redundantly marked static).
What does 'public static void' mean in Java?
It's three completely different things:
public
means that the method is visible and can be called from other objects of other types. Other alternatives are private
, protected
, package
and package-private
. See here for more details.
static
means that the method is associated with the class, not a specific instance (object) of that class. This means that you can call a static method without creating an object of the class.
void
means that the method has no return value. If the method returned an int
you would write int
instead of void
.
The combination of all three of these is most commonly seen on the main
method which most tutorials will include.
Why static fields are called static ?
It's a hold over from C++ and C before that. In the C context, one of its meanings was for variables that keep their value between invocations. I presume that's where 'static' comes from - it doesn't reset the value (as opposed to a const where the value cannot change)
what is static method why use this in Display class in java
Static methods are those methods can be called without creating its object.
It can be invoked using its class name.
Eg. Math.sqrt(25);
Where Math is the class name, not an object and static methods can access only static properties of the class.
What is the purpose of static keyword in this simple example?
In this Example,Static is used to directly to access the methods.A private static method defeats the purpose of "Data hiding".
Your main can directly call test1 method as it is also Static,it dosn't require any object to communicate.Main
cannot refer non-static
members,or any other non-static member cannot refer static member.
"non-static members cannot be referred from a static context"
You can refer This thread for more info about Static members.
What is the purpose of creating static object in Java?
The static keyword in Java means that the variable or function is shared between all instances of that class, not the actual objects themselves.
In your case, you try to access a resource in a static
method,
public static void main(String[] args)
Thus anything we access here without creating an instance of the class Flavor1Demo
has to be a static
resource.
If you want to remove the static
keyword from Demo
class, your code should look like:
class Flavor1Demo {
// An anonymous class with Demo as base class
Demo d = new Demo() {
void show() {
super.show();
System.out.println("i am in Flavor1Demo class");
}
};
public static void main(String[] args) {
Flavor1Demo flavor1Demo = new Flavor1Demo();
flavor1Demo.d.show();
}
}
Here you see, we have created an instance of Flavor1Demo
and then get the non-static
resource d
The above code wont complain of compilation errors.
Hope it helps!
Why out variable in System class is static ?
That is because static variables can be used without instantiating the class. So, something as simple as printing for which you use out
mostly you don't have to create an instance of System
class and can access the variable with just class name.
Related Topics
Java System Preferences Under Different Users in Linux
Swt Browser No More Handles Error
Find Directory for "Application Data" on Linux and MACintosh
How to Store Unix Permissions in a Zip File (Built with Apache Ant)
How to Get Desktop Class Supported Under Linux
Execute a Linux Terminal Command in Java
Connecting to Access Database from Linux
How to Run Java Application on Startup of Ubuntu Linux
Environmental Variables Not Seen in a Cron
Java Animation Programs Running Jerky in Linux
Accessing Linux Local File System from Java Web Application
Java.Lang.Outofmemoryerror: Java Heap Space
Android - Save Image into Gallery
Image Encryption/Decryption Using Aes256 Symmetric Block Ciphers