I am very confused between static and not static.
I really can't understand where to use Static variable or static methods and where to use non-static variables or non-static methods?
Waiting for a helping hand
I am very confused between static and not static.
I really can't understand where to use Static variable or static methods and where to use non-static variables or non-static methods?
Waiting for a helping hand
This is for Java. C++ is likely similar in the first two respects, perhaps more, though I cannot be sure.
Static: Applies once for the class. Accessed with FooClass.staticField
or FooClass.staticMethod(args)
.
Non-static: Applies per-instance. Accessed with fooInstance.field
or fooInstance.method()
. fooInstance
is an instance of FooClass
and was obtained with a constructor or factory.
Generally, constants such as a Color
class's YELLOW
constant is declared static final FooClass
.
Another use of static
is the static initializer:
class FooClass
static{
//Code here gets called once when the class is loaded.
}
In Java, static refers to the class as a whole, not to objects (instantiations) of the class. For example, if I created a class called Bicycle:
instance
variable could be its colour, or registration number. static
variable, as it is class wide (note, this should really be made a constant with the final
keyword as well, as it will not change).It's the same with methods. Static methods are class-wide, non static methods are specific to that object.
static
means slightly different things in C++ and Java, but the rules for static class members (methods and variables) are basically the same: Members that are static apply to the whole class, and members that are not static apply to individual instances of the class.
For a common example, it's usual in Java to have one logger that belongs to the entire class, since each instance doesn't need its own copy. So you see
private static final Logger logger = LoggerFactory.getLogger("loggerName");
(The final
here means the same thing as const
in C++.)
Methods that are static are ones that don't apply to a specific instance. A common example is Integer.parseInt(String)
, which isn't called on an existing Integer
because it's turning a String
into an Integer
.
Variables and methods that apply to individual instances (such as length()
or name
) are not static.
I dont know how many times this question has to be answered on stackoverflow Accessing Static variables
Firstly read this article,also use wikipedia,then look for a book called Headfirst Java (Sierra & Bates) and also Java How To Program(Deitel,Deitel). These books will be of use to u. After u've read and think u've grasped the concept give urself a project to do where this will be implemented. You will understand it faster and better if u practice so get reading and get coding.
Understand it this way, static variables are shared by all objects unlike non static variables which are different for different objects.Consider a scenario, where you are developing a student class ,different objects of this class will represent different students each having unique roll no, different names(or same). Now, all of this students represent the same college.
class Students
{
int roll;
String name;
int collegecode;
public static void main (String args[])
{
Students o1= new Students();
o1.roll =12;
o1.name ="james"; // This is a bad practice,fields should be private
o1.collegecode = 7523 ;
Students o2 = new Students();
o2.roll =13; // getter, setter methods should be used
o2.name ="Michael";
o2.collegecode= 7523;
}
}
Since collegeid would be same for every students (objects), it can be independent of objects and can be made static.Static methods/variables are shared by all objects and can be accessed by classname.variable
or classname.method
. Although , you can access a static variable/method as object.method
or object.variable
. (But there is no point in that, you can refer it like o1.staticvariable, o2.staticvariable and so on,it all refers to same variable)
If you make collegecode static in the above example,it will look like
static int collegecode;
o1-------> college code <---- o2
roll (12) roll(13)
name(James) name(Michael)