I have my utmost respect to my professor that he knows everything about Java. But I am having difficulty understanding his explanation about constructors and set/get methods. What are they being used for? If someone can explain a little more hopefully I can get the AHA moment.
Used to initialize an object of the class and any members. Called only once for the lifetime of the object being initialized:
Used to get or set values of object members respectively. Unlike constructors, set methods can be used to initialize member values more than once:
|show 2 more comments|
Plainly and simply:
These concepts are valid in any object oriented language, not just Java.
Class constructors are called just once, when the class is instantiated into a live object. Constructors are typically used to force the new, live object into a known state before anything else can act on it.
For example, if you have a Ball class with a size member variable whenever a new Ball is created the constructor should ensure that size is initialized to some safe value and not some random number. Alternately your Ball constructor can require someone making a new Ball to specify how big a size they want.
Get/Set methods are great for protecting live objects from just anyone poking around and making stupid or dangerous changes. They are also great for allowing your class to do extra work required when some property is change.
For example, your Ball has a setSize(int newSize) so someone can pump it up bigger. This set() function can make sure newSize is a safe value and also do extra work required inside your class like adjust a hidden airPressure member variable used in the hidden ball physics simulation that a user of the class shouldn't have to worry about.
Another example, your Ball has a getMaterialRequired() that returns how much rubber is needed to make a Ball based on what its size is. Someone outside should not know how the Ball knows how much rubber is required, all they need is to know how much. So the get() function both hides the complexity and can do extra work to create results whenever needed.
A constructor initializes an object. In other words, when you call the constructor method (function), it sets the instance attributes of the class to what is passed in as the parameters to the
Here's a way to think about it. A class is an idea. An object is the actual thing that the class describes. An analogy would be that the difference between an object and a class is much like the difference between a tree (a physical object with leave and roots and branches) and the idea of a tree (a mix of remembered physical sensations and words that exist in the mind).
In simplest terms, the constructor turns the idea into a thing. The constructor is a static member of the class, which means it belongs to the class (the idea) not the object (the thing) represented by the class.
Now, getters and setters are a completely different topic. To understand them, I would recommend that you review the "public", "private", and "protected" keywords. Any explanation of what getter and setter methods do will make no sense until you understand what these keywords do and why they are in the language. (Hint: The typical use case for Java in the real world is on projects that involve multiple programmers building software in teams. Private and protected members serve to hide complexity from developers who are using classes that have already been compiled and, ideally, documented. When object oriented programming is done well, the resulting classes and objects should have a single, logical pattern of use that can be understood without knowing the details of the implementation.) Once you understand all that, the following will make sense.
A getter is a method that provides read access to a private or protected property (variable).
A setter is a method that provides write access to private or protected property. A setter may include some data filtering capability.
Putting a getter on a class without a setter effectively makes a private or protected property read only to users of the class.
I just wanted to help give an example of how getters/setters might look in some practice
You (presumably) have some sort of credit card. You can adjust your available balance by making new charges or paying money back. You need to see your balance, but you shouldn't be able to just set it to whatever you feel like. We solve this problem by not letting you access your balance directly, but rather through a (i.e.
Now, you're in college, which isn't permanent. At some point, you'll graduate and move. You need to change your address information with the credit card company. You can't just set your address to anything you want ("Send my bills to this address that corresponds to absolutely nowhere."). You need to update that value, so the credit card company writes some code to update that address. So you have things like
This makes sure that every time you try to change your address, it's to the real thing. You can also use methods to change values without just overwriting them. For instance, with a credit card, you could have methods like:
These don't "replace" the old value of a variable like in most setter methods, but they do update the value with checks as needed (like not letting you exceed a credit limit).
A constructor creates the space in memory that will store your object that you are creating. That would be what a constructor with no arguments does.
The constructor can also be written to take arguments, and execute code that will be executed when you create the object. This so you can ensure that the object is in a specific state after it is created, but before it is used.
Get and set methods simply wrap members
This is so you can hide internal state, or do verification before some value is retained. For example a property of type int may need to be between the values of 1 and 10. In your setter you could verify that the value is indeed between 1 and 10, and then take appropriate action if it is not.
In summary, they both exist to allow you better control over what a class can and cannot do. Here is a full class that uses both.
You should be able to see from the example what a constructor is for, it creates the object and puts it in a valid initial state. The property getter and setter methods allow you to control how a value is set. This class makes it very easy for someone to use your volume controller in an application without worrying if it will throw an exception and break their code. This is the point of both constructors and property setters and getters.