0
Java OOP Counter the object
How can I count the number of object by using Static ?
3 Antworten
+ 2
public class MyObject{
static int counter = 0;
public MyObject(){
counter++;
}
}
Inside main:
MyObject.counter -> print it or assign it to a variable
+ 1
here is a quick reference which explains your question and answer
https://www.google.com/amp/s/www.geeksforgeeks.org/count-number-objects-using-static-member-function/amp/
+ 1
Create a static variable and increment it inside the constructor of the class.