Static Properties and Methods in Java
Java Just After the Basics – Part 5
Forward: In this part of the series, I talk about what is known as static properties and static methods.
By: Chrysanthus Date Published: 2 Sep 2012
Introduction
Static Property
A static property is a property that can be used directly by the class without instantiation, and its value is the same for all objects at all times. If the value of a static property changes, the change is the same for all already instantiated objects and the class itself (and objects still to be instantiated). See illustrations below.
Declaring a Static Property
You declare a static property just as you declare any other property, but you precede the declaration expression with the reserved word, static (and a space). The syntax is:
static Type Ident;
Example
The following class illustrates the use of a static property:
class MyClass
{
static int sameAll;
}
class StaticProp
{
public static void main(String[] args)
{
MyClass.sameAll = 5;
MyClass myObj = new MyClass();
myObj.sameAll = 6;
System.out.println(MyClass.sameAll);
}
}
In the code, you have a class called, MyClass. This class has just one property, which is the static property. You can assign a value to the static property inside the class definition (description) or in the main method (or some other area). In this program, the static property has been assigned its first value in the main method. The syntax to access a static property is:
ClassName.property
That is, you begin with the class name, then a dot and then the property name; there is no need for instantiation, and so there is no object or object name, after all, the property value is the same for the class and all old and new objects, at all times.
You instantiate an object from the class that has the static member, in the normal way. The second statement in the main method illustrates this. You can also access the static property of an instantiated object in the normal way. The third statement in the main method illustrates this. However, changing the value as this line has done means changing the value for the class (definition) and any already instantiated object and any object that is still to be instantiated.
The third statement in the main method displays the static property value. It used the class name; it did not use the object name. To use the class name to access the static property, you begin with the class name. This is followed by the dot and then the property name, as obtained from the access syntax above. This shows how you can access a static property with the class name directly and without using an object; this is like accessing the property in the class description (definition). The static property is a kind of global property.
You can still use the instantiated object to access the static property. So, the print statement could have been:
System.out.println(myObj.sameAll);
where the static property has been accessed in the normal way.
The following example illustrates that the static data member is the same for its class and its instantiated objects and if you change it through an object or through the class (MyClass.sameAll), the value is changed to the same new value, for all the objects and the class:
class MyClass
{
static int sameAll;
}
class StaticProp1
{
public static void main(String[] args)
{
MyClass.sameAll = 5;
MyClass.sameAll = 6;
System.out.println(MyClass.sameAll);
MyClass obj1 = new MyClass();
obj1.sameAll = 7;
MyClass obj2 = new MyClass();
System.out.println(MyClass.sameAll);
System.out.println(obj1.sameAll);
System.out.println(obj2.sameAll);
}
}
Read through the code and try it. The second statement in main changes the value using the class name. The third statement displays the changed value. The fourth statement instantiates an object from the class. The fifth statement changes the value using the instantiated object. The sixth statement instantiates a new object. The rest of the statements display the value using the class and then the two different objects. These three lines, all display the same value (7) confirming the purpose of static property.
A static method is a method that can be called, without object instantiation, just like the static property. The following program illustrates this:
class MyClass
{
static void mthd(int no)
{
System.out.println(no);
}
}
class StaticMeth
{
public static void main(String[] args)
{
MyClass.mthd(16);
}
}
To access a static method without an object (instantiation), use the following syntax (similar to that of static property):
ClassName.methodName(args list)
That is it for this part of the series. We stop here and continue in the next part.
Chrys