Php updating a static variable best christian dating sites uk
A constructor is just a special method that's run whenever an object is created.
The constructor has the same name as the class and no return type, not even void.
The static count variable is set to 0 at the instant it's created, right where it's declared.
But every time a the constructor is invoked subsequently (by creating a new object in the 'main' method), it increments count.
If we want to do this in the class itself, how can we make the variable stick around from object to object?
Another classic use of static is to keep count of how many objects are created from a given class.
And of course, the ID of the first object will be 1, the ID of the third object 3, etc.
Each object has its own separate ID, which is set in the constructor using the static 'count' variable.
Let's say you define a class like the following, with just one public static variable.
Stuff.java: We've declared a class called ' Stuff' and given it one public static variable of type String.