0
What does instance of a class mean?
Static method can run without creating instance of class containing main method
3 Respostas
+ 4
Think of a class as a blueprint for an object. You can create multiple instances of these objects in memory. Each object instance will have memory allocated to their specific instances. Instance methods will be able to access instance properties using the "this" keyword.
Static members are tied to the class itself, which only a single instance will ever exist in memory. Static members cannot access instance members because they occupy different memory spaces.
Below, I'm referring to pseudo code to help illustrate simple examples.
Think of a Person class and multiple instances of Person objects.
p1 = new Person("Minnie", "Mouse")
p2 = new Person("Mickey", "Mouse")
p1 and p2 are two object instances of the Person class.
p1.getFullName() is an instance method that returns the combined values of two instance properties using 'this'.
Person.getFullName(p1) is a static method that does the same thing, but it will require the instance object to read values from.
It's a matter of designing what makes sense.
+ 2
Instance of a class is nothing but an object. A static member of the class belongs to the class and not to a particular instance so they can run without creating an instance of the class.
+ 2
I may have created more questions than answers. Hopefully, this will have made sense enough to sort out some differences between instance and static class members. It's tough to try to fit so much into a short post with limited characters.