What is difference between static method and static
variable?
Answer Posted / joh-b tanzania
The static keyword denotes that a member variable, or method, can be accessed without requiring an instantiation of the class to which it belongs.
In simple terms, it means that you can call a method, even if you've never created the object to which it belongs! Every time you run a stand-alone application (which requires a static main method), the virtual machine can call the main method without creating a new application object. Of course, unless the application's methods are all static, you will need to create an instance of it at some point.
With regard to member variables, it means that they can be read from, and written to, without creating an object. You may have noticed that many classes create constants that can be read, without creating an object.
static final int VERSION = 2;
Static member variables are shared by all instances of the class to which they belong. When writing classes, this can be a handy feature. Consider the following example, where a counter is used to track how many myObject instantiations have taken place.
public class myObject
{
static int objectCount = 0;
public myObject()
{
objectCount++;
}
public String toString()
{
return new String ("There are " + objectCount + " objects");
}
}
| Is This Answer Correct ? | 3 Yes | 1 No |
Post New Answer View All Answers
How is it possible in java programming for two string objects with identical values not to be equal under the == operator?
Can we split string with in java?
Is null a string?
What is console based application in java?
What access modifiers can be used for methods?
How do you use wildcards?
What is a double?
What are the two basic ways in which classes that can be run as threads may be defined?
What is generics in java interview questions?
What is the difference between and ?
Why java is a platform independent? Explain
Explain about transient variables in java?
Are arrays dynamic in java?
I want to re-reach and use an object once it has been garbage collected. How it's possible?
How does list work in java?