Java Concepts: Objects; Classes, Variables, and Methods
tl;dr (too long; didn’t read) – Java uses classes which declare variables that store information and are modified/retrieved by methods once an object of that class is instantiated. The next post will contain code complimenting these concepts. // Continue if you want to read more. /* Java is an object-oriented programming language, but just what is an object? To find out, we must first delve into the concept of classes. Classes are “templates” of objects. These “templates” dictate all the variables, or “data,” and methods, which are essentially “actions,” that the still unknown object will have. The class will have a name, along with variables and methods. The only time you may see these names is when you are instantiating an object from the class or calling (“executing”) a method of an object, which can only be done because of the code in its originating class. To instantiate means “to create an instance of,” which, if used in real-world context (although awkward), would be akin to saying “make me one of those, and make it just like the others.” Wait, I know I might have lost you by this point, but you needed to understand what variables, methods, and classes were before an object made any sense. We know a class is a template, right? Well, consider that you can instantiate multiple objects based on this class. What this means is that they are absolutely identical in every way except for maybe their data and name, which we normally call a “reference” (because it’s actually referring to a location in memory, a hexadecimal number that would glaze your eyes over!). If a metaphor would help, they’re all identical twins that know different things, but can do the same actions! So, even though you declare variables and methods in a class, when a variable is modified or a method is run they are not affecting the class but the object that was instantiated! Now it makes sense suddenly, doesn’t it? But wait! How does this “instantiation” thing work you ask? Good question, through a type of method (yes, these are methods) called constructors! What they do is (hold onto your hats because I’m using the next word in a different sense) instantiate the variables of an object so that the object knows what data is has in case it has to do something. Now wait, we just instantiated the object, why instantiate the variables; moreover, what’s the difference? The answer is we haven’t instantiated the object yet, we need to set values to the variables so that the object has data. Only then should the object be considered instantiated! So now we know what an object is: an instance of a class that has variables and methods dictated by the class. You might be wondering about methods at this point. Methods have, traditionally, two types: accessors, and mutators. What’s an Accessor? Well, quite simply, it accesses and returns (“gives back”) data stored in the object. Nothing too special, right? Mutators, on the other hand, can do all sorts of creative things but normally modify the data of an object in some way. You won’t always get data back from these, nor will they always tell you whether they successfully executed, but their purpose is clear: when something gets changed, this is the type of method it becomes. Something to note is that this is simply a naming convention, not a requirement. You can call them “Destroyer” methods and nobody can really say anything about it, as the compiler will not complain and your code will still run. Despite this, I encourage you to think of others who may read your code, and to try your best to keep to a standard and/or convention! The next post will actually involve some code examples of all these concepts, which will be linked here upon creation. If you’re still having trouble, don’t fret, keep reading up on the concepts as much as you can and, if necessary, ask for metaphors or similes! */
Read more