View RSS Feed

Core Java

Static Fields and Methods

Rate this Entry
by , 03-02-2012 at 04:16 PM (2257 Views)
Methods and fields might be declared static. This is also possible for C++. Only one copy is present for the complete class when field is static, rather than one copy for all class instances. If class instance is not present even then fieldís copy is available. For example, given field addition in List class:

static int numLists = 0;

And, the given statement addition in the List constructor:

numLists++;

When new List object creation takes place, increment of the variable of NumList is done. Hence total created Lists are counted when a program is executed. All List instances access this variable and similar variable is accessible, not the individual copies.

Methods need to be static in case accessibility of non static field is not possible. Donít call non static method. In C++ Free function methods shall be static. Logically associated methods present with a specific class shall be static as well. These methods are involved in manipulation of the Static field class.

Public static field is accessible by usual notation, outside the class:

class-object.field-or-method-name

Instead of class object name, class name might be used:

class-name.field-or-method-name

Submit "Static Fields and Methods" to Facebook Submit "Static Fields and Methods" to Digg Submit "Static Fields and Methods" to del.icio.us Submit "Static Fields and Methods" to StumbleUpon Submit "Static Fields and Methods" to Google

Categories
Uncategorized

Comments