What does instance of a class mean? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What does instance of a class mean?

Static method can run without creating instance of class containing main method

21st May 2020, 9:53 AM
Priyanka Macha
Priyanka Macha - avatar
3 Answers
+ 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.
21st May 2020, 1:27 PM
David Carroll
David Carroll - avatar
+ 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.
21st May 2020, 9:56 AM
Avinesh
Avinesh - avatar
+ 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.
21st May 2020, 1:30 PM
David Carroll
David Carroll - avatar