It is dependent on the requirement of your usage of the Object of that class
Static member variables are local to the class. That is, there is only one instance of a static member variable, regardless of how many objects are instantiated from the class. As such, they must be declared inside the class, and defined outside of the class.
Static MethodsStatic keyword when used with a method, specifies that this method belongs to the class and not a particular instance of the class (a.k.a object of the class)Ex:public class StaticTest {public static String getAuthorName() {return "Anand";}}Here getAuthorName is the static method and it can be accessed without instantiating an object of the class StaticTest. You can access this method as:String authorName = StaticTest.getAuthorName();Tip: It is important to know that, a static method cannot and I mean CANNOT access instance variables or methods of the class. It can only access other static methods and variables.For Ex:public class StaticTestBad {private String name = "Rocky";public static String getAuthorName () {return name; //Cant work!!!}public int getAge(){return 28;}public static int getAuthorAge() {return getAge(); //Cant Work Again!!!}}If you try to compile this class, you will get compilation errors at the two lines that I have commented out.Static VariablesThe static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.
Static data members are different from automatic ones in the way that their lifetime is equals to the lifetime of your program. Even if you have declared static members inside of function (class) other than main();
A static variable is a variable that is allocated at compile time and that remains in memory for the entire duration the program remains in memory. Contrary to the misleading answer given below, static variables ARE variables, they are NOT constants; the value is initialised at compile time but can be modified at runtime whenever the variable is within scope. Static constants are also possible, but they must be explicitly marked constant -- otherwise they are implicitly variable.As with ordinary variables, the scope of a static variable is dependant upon where it is declared. This could be at global scope, file scope, function scope, namespace scope or class scope. However, the scope only determines the visibility of a static variable. The variable exists at all times even when not in scope so its value can persist across scopes, but the value can only be modified when the variable is currently in scope.Global scope means the static variable has external linkage and is accessible to all code with access to the definition. File scope limits visibility to the translation unit that defines it. Function scope limits visibility to the function that defines it. Namespace scope limits visibility to the namespace that defines it, but you can still gain external access via the scope resolution operator (::).Class scope is the most complex scope because as well as scope resolution, the variable is also subject to the class access specifier (public, protected and private) associated with it. However, another key difference is that static class member variables are scoped to the class itself, not to any instance of the class. Thus they are visible (if not accessible) even when no instances of the class exist.***************PREVIOUS ANSWER*********************A common misconception about and misuse of the static qualifier is:A static variable is program variable that does not vary... go figure.Static variables have the same value throughout run time. They can be changed at design time only.This is actually a better description of the const modifier.'static' is used in C programs to declare a variable that should exist throughout the lifetime of the program. When the program starts executing, all allocations are made for static variables. It has the added side-effect of making the variable not visible outside the scope of the module in which is declared. The explanation in the answer below this one describes this well.For more information specific to java see: http://mindprod.com/jgloss/static.html Answerstatic is an access qualifier that limits the scope but causes the variable to exist for the lifetime of the program. This means a static variable is one that is not seen outside the function in which it is declared but which remains until the program terminates. It also means that the value of the variable persists between successive calls to a function. The value of such a variable will remain and may be seen even after calls to a function. One more thing is that a declaration statement of such a variable inside a function will be executed only once.For usage try following: void fun() { static int fnvalue=0;//Executed once printf(" \n%d",fnvalue++);// Value changed retains for next call }this code behaves like a sequence generator.One more advantage of static variables is that, since they are created once and then exist for the life of the program, the address of the variable can be passed to modules and functions that aren't in the same C file for them to access the variable's contents. This has several advantages in programming.For further confusions or details write me: rupesh_joshi@sify.com,rupesh.joshi@gmail.comC++ and Java In Object-oriented languIages like C++ and Java, the static keyword has additional practical value. If you define a class-level variable as static, that variable is accessible whether there is an instance of the class or not. Furthermore, all instances of the class share the same single value. This is used in a number of ways.Sometimes you'll have a class where you want all the instances to share a single variable (EG a bus class where all buses in the system shared the same totalRiders variable.) Often you'll define constants as static final, because they are frequently used as parameters for the constructor of the class.Methods can also be static, which means they can be called without an instance of the class. Most often this is used to make a class act like an old-school function library. The Java Math class is a great example of this. You never instantiate the Math class, but all of its methods are static.A static member is almost always called with the class name rather than the instance name."static" in programming and databases means "constant--never changing". So, a static variable could be the "node name" or "database name". Once those are set, they cannot be changed.
In the case a Java, if a variable at the class level (called a "field" in Java) is declared as static, a single copy of such a variable exists, no matter how many objects are created for the class. This lets you share information between different objects; you can also access such a variable without creating a single object, using the class name. A good example are the fields Math.PI and Math.E, i.e., fields in the "Math" class which you can access without creating an object based on the class.
The static modifier tells the system that this particular variable belongs to the class and does not belong to any specific instance of the same. The class will contain only one instance of the static variable irrespective of how many objects of the class you create.
c: A static function is visible only in the source file containing it. A static member is not defined. c++: A static function is visible only in the source file containing it. A static member variable is common to all instances of a class. A static member function is used to access static members.
A Static method in Java is one that belongs to a class rather than an object of a class. Normal methods of a class can be invoked only by using an object of the class but a Static method can be invoked Directly. Example: public class A { ..... public static int getAge(){ .... } } public class B { ..... int age = A.getAge(); } In class B when we wanted the age value we directly called the method using the instance of the class instead of instantiating an object of the class. Tip: A static method can access only static variables. The reason is obvious. Something that is common to a class cannot refer to things that are specific to an object...
Yes. Static data members are local to the class in which they are declared. Thus all instances of the class share the same variables (unlike non-static data members where each instance of the class has its own set of variables). Moreover, since static data members do not belong to any instance of the class, they are accessible without the need to instantiate an instance of the class, and like all other static variables, remain in scope for the entire duration the program is running. Also, as with all other static variables, they must be initialised at compile time from outside of the class declaration. Usually this is done from the class CPP file.Static member functions are similar to static data members in that they are local to the class, rather than to an instance of the class. Since they do not belong to any instance of the class, they do not inherit an implicit this pointer. As a result, they are accessible without the need to instantiate an instance of the class and will remain in scope for the entire duration the program is running.It is not unusual for a class to have both static data members and static member functions. They can be likened to global variables and global methods, but scoped to the class. However, their visibility can be restricted by the access specifiers enforced upon them (public, protected or private). Although static member functions cannot access instance methods and instance variables (unless an instance is physically passed to them as an argument) they have unrestricted access to the static data members of the class, as do all instances of the class and friends of the class.A classic example of static member functions and static data members in the same class is when one needs to maintain a count of all instances of a class. All the class constructors must increment the static counter while the destructor must decrement it. A static member function such as GetCount() can then report the number of instances currently instantiated, even when there are no instances.There are many other uses, but the golden rule is that they must be related to the class in which they are declared. If their purpose is simply to provide global functionality then declare them as such, or (better) limit their scope by declaring them in a separate class specifically for that purpose with private constructors to prevent any instances from being created (since none would be required).
Static members should be used whenever you have a member that is logically considered part of the class, but is not associated with any instance of the class. They are bit like global variables and functions but there's more control over their visibility outside of the class.Static member methods can be thought of as being global functions that are scoped to the class (rather than to each instance of the class). As such, they do not inherit an implicit this pointer and can therefore be called without instantiating an instance of the class (access specifier permitting). However, any and all instances of the class have unrestricted access to all the static member methods of the class.Static member variables can be thought of as being global variables that are scoped to the class (rather than to each instance of the class). That is, all instances of the class share the same set of static member variables amongst them, rather than each having their own (as they would with non-static member variables). Static member variables are also accessible to static member functions. As with all other static variables, they are initialised at compile time (which must be done outside of the class declaration), and will remain in scope for the entire lifetime of the program.All static members are subject to the public, protected or private access specifiers, so while they can act like global variables and functions if declared public, their visibility outside of the class can be limited to private or protected access.Possible uses for static members are many and varied. By way of an example, suppose you want to maintain a read-only count of all instances of a class. The following framework demonstrates how this can be done with static members:class Object{public:Object(){++s_instances;}~Object(){--s_instances;}static unsigned int GetInstances(){return(s_instances);}private:static unsigned int s_instances;};// Static member variable initialiser (evaluated at compile time):unsigned int Object::s_instances=0;int main(){cout
Static methods can refer to instance variables and methods, as long as they are static as well. The reason you cannot mix nonstatic and static class members is because they have different scope. Static members are independent of any particular object, whereas nonstatic members are unique for each object that is instantiated. To clarify, consider the following class:class A{int x=0;static int y=1;}If three instances of A are created, 3 instances of x will also be created. Changing one of those x's has no effect on the others. However, only one instance of y will be created, regardless of how many A's are ever created. A change in y will be reflected in every A.Now, if you were to call A.y+=x, which x would you be referring to? 3 A's have been created, each with possibly different values of x. Because of the ambiguity of this, you will get a compiler error whenever you mix static and nonstatic members.
static: we can use the keyword static either to method or to a variable. when we declare to a method,(eg: public static void main(String args[]),we can use this method without any object. when we use to a variable,there will be only one instance of that variable irrespective of how many objects that get created of that class. Final: Usage of final to method or to a variable makes them as constant. It's value cannot be changed...