Presentation is loading. Please wait.

Presentation is loading. Please wait.

Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class.

Similar presentations


Presentation on theme: "Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class."— Presentation transcript:

1 Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class rather than with an object. There are times when a class wants to have a variable that is shared by all the objects that comes from the class. If any one of the objects changes the value of this variable, the change is reflected in the state of every object from the class. The variable is declared and assigned a value as though it were an instance variable; however it uses the keyword static in its declaration. Static variables are also called class variables and like all instance variable, they are declared private. The static Modifier

2 The static Modifier Static methods (also called class methods) can be invoked through the class name rather than through a particular object For example, the methods of the Math class are static: Math.sqrt (25) To write a static method, we apply the static modifier to the method definition The static modifier can be applied to variables as well It associates a variable or method with the class rather than with an object Read after math example: We don’t have to instantiate an object to call a static method as we see in the Math.sqrt example.

3 Static Variables Static variables are also called class variables
Normally, each object has its own data space, but if a variable is declared as static, only one copy of the variable exists private static float price; Memory space for a static variable is created when the class in which it is declared is loaded All objects created from the class share static variables The most common use of static variables is for constants

4 Static Methods class Helper public static int triple (int num) {
int result; result = num * 3; return result; } class Helper Because it is static, the method can be invoked as: value = Helper.triple (5);

5 Static Methods The order of the modifiers can be interchanged, but by convention visibility modifiers come first Recall that the main method is static; it is invoked by the system without creating an object Static methods cannot reference instance variables, because instance variables don't exist until an object exists However, a static method can reference static variables or local variables Static methods and static variables often work together


Download ppt "Static is one of the modifiers that determine variable and method characteristics. The static modifier associates a variable or method with its class."

Similar presentations


Ads by Google