I'm beginning to program in Java.
public static void main(String[]args)
A book said that I should use static in this case, but doesn't clearly say why I should or what it means.
Could you clarify this?
I'm beginning to program in Java.
public static void main(String[]args)
A book said that I should use static in this case, but doesn't clearly say why I should or what it means.
Could you clarify this?
A static method is one that applies to the class a whole, not any particular member. .goExtinct() would be a method of the Duck population as a whole, not any particular duck. main is public and static because is has to always be available, and its not part of any particular class.
Please see a nice description on Wikipedia
For example, notice how in the Math class, you can say things like
without having to say
These are static methods since you don't need an instance. Instance methods are those methods that require you to have an instance of a class.
Usually, you have to have an object, an instance of a class, in order to call methods on it, for at least two reasons:
You create object instances by calling the class' constructor:
Static methods are methods that are not attached to object instances. They can be called by just naming the class. As a result of this they
Many people consider static methods a bad design pattern, and advise to not use them (except for
public static void main
) Look up the singleton instance pattern for an alternative.The concept of
static
has to do with whether something is part of a class or an object (instance).In the case of the
main
method which is declared asstatic
, it says that themain
method is an class method -- a method that is part of a class, not part of an object. This means that another class could call a class method of another class, by referring to theClassName.method
. For example, invoking the run method ofMyClass
would be accomplished by:On the other hand, a method or field without the
static
modifier means that it is part of an object (or also called "instance") and not a part of a class. It is referred to by the name of the specific object to which the method or field belongs to, rather than the class name:As each instance could have different values, the values of a method or field with the same name in different objects don't necessarily have to be the same:
Combining the two concepts of instance and class variables. Let's say we declare a new class which contains both instance and class variables and methods:
The above class has an instance variable
instanceVariable
, and a class variableclassVariable
which is declared with astatic
modifier. Similarly, there is a instance and class method to retrieve the values.The constructor for the instance takes a value to assign to the instance variable as the argument. The class variable is initialized to be
42
and never changed.Let's actually use the above class and see what happens:
Notice the different ways the class and instance methods are called. The way they refer to the class by the name
AnotherClass
, or the instance by the nameac1
. Let's go further and see the behavioral differences of the methods:As can be seen, an instance variable is one that is held by an object (or "instance"), therefore unique to that particular instance, which in this example is the objects referred to by
ac1
andac2
.A class variable on the other hand is only unique to that entire class. To get this point across even better, let's add a new method to the
AnotherClass
:Then, run the following:
Although
getClassVariableFromInstance
is an instance method, as can be seen by being invoked by referring to the instancesac1
andac2
, they both return the same value,42
. This is because in both instance methods, they refer to the class methodclassVariable
which is unique to the class, not to the instance -- there is only a single copy ofclassVariable
for the classAnotherClass
.I hope that some what clarifies what the
static
modifier is used for.The Java Tutorials from Sun has a section called Understanding Instance and Class Members, which also goes into the two types of variables and methods.
There will be times when you will want to define a class member that will be used independently of any object of that class. Normally a class member must be accessed only in conjunction with an object of its class. However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( ). main( ) is declared as static because it must be called before any objects exist. The two types of static members are static fields and static methods:
A field that’s declared with the static keyword, like this:
The position of the static keyword is interchangeable with the positions of the visibility keywords (private and public, as well as protected). As a result, the following statement works, too:
As a convention, most programmers tend to put the visibility keyword first.
The value of a static field is the same across all instances of the class. In other words, if a class has a static field named CompanyName, all objects created from the class will have the same value for CompanyName.
Static fields are created and initialized when the class is first loaded. That happens when a static member of the class is referred to or when an instance of the class is created, whichever comes first.
A method declared with the static keyword. Like static fields, static methods are associated with the class itself, not with any particular object created from the class. As a result, you don’t have to create an object from a class before you can use static methods defined by the class.
The best-known static method is main, which is called by the Java runtime to start an application. The main method must be static, which means that applications run in a static context by default.
One of the basic rules of working with static methods is that you can’t access a nonstatic method or field from a static method because the static method doesn’t have an instance of the class to use to reference instance methods or fields.
In any object oriented programming language like Java or C++ you create classes which at the very basic level are like BluePrints of a building. You can look at a blueprint and determine how various components are connected but you cannot actually live in it. It's the same with classes and object. Classes are blueprint and you create an instance of a class which is called an Object. For the same blueprint you can have multiple buildings , same way for one class you can have multiple objects. An Object is an instance of a class. Each method in a class can be called on an Object or an instance of a class, whereas for calling static methods you actually don't need an instance, you can directly call ClassName.method() without actually creating an instance of a class.