travishaberman
asked on
What does the keyword static do/mean in java?
I am familiar with C++ and I am starting to code in Java..
1) What does the word static do in the cases below?
2) Is that also the case when used in a function header?
3) When is it proper to use it? Why?
4) Any information you think may be useful for me about it that I did not ask?
public static void main(String[] args)
{ }
Thank you experts,
-TH
1) What does the word static do in the cases below?
2) Is that also the case when used in a function header?
3) When is it proper to use it? Why?
4) Any information you think may be useful for me about it that I did not ask?
public static void main(String[] args)
{ }
Thank you experts,
-TH
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1. Static Member Variable
2. Static Execution Block
3. Static Method
Illustrated by :
public class Foo
{
//--- this is a static member variable, means it is shared by all instances of this class
private static int instanceCount;
//--- this is a static execution block, will run exactly once, when the class is loaded
static
{
System.out.println("Foo has been loaded");
}
public Foo()
{
incrementNumInstances();
}
//--- this is a static method
protected synchronized static void incrementNumInstances()
{
instanceCount++;
}
public static int getInstanceCount()
{
return instanceCount;
}
}