views:

112

answers:

3

Okay so I have an assignment where I have to create a class with a set of private properties, which I have done. It gets tricky because I'm fairly new to java programming (or programming in general) and am not very practiced in encapsulation. I have not used getters/setters or constructors before. I understand the getter/setter methods, but have yet to understand any of the information online for constructors, or even understand their purpose.

I have to create getters/setters and constructors for each of the properties so it would seem I should learn to use these methods. If anyone could give me an example of a class with a constructor and explain what the constructor is doing, and why I would use it it would be nice.

Thank you ahead of time.

+4  A: 

First off, welcome to the programming world :)

Lets look at a simple example.

Say you want to program a class to represent a Square. When you create a Square object, you have to give the Square certain properties, such as the length of each side. This is where constructors come into play.

The class layout looks like this, note the length_of_each_side variable is not assigned a value.

public class Square {
     int length_of_each_side;

     public Square() {
     ...
     }

     public Square(int length) {
     ...
     }
}

You can have a default constructor that will give you a predefined Square if you do not specify the size of the square.

public Square() {
     length_of_each_side = 1;
}

or you can allow someone to specify the size of the square

public Square(int length) {
     length_of_each_side = length;
}

If I want to create a pre-defined Square (length 1), it would look like this

Square mySquare = new Square();

If I want to create a Square and specify the length of 55, it would look like this

Square mySquare = new Square(55);
Kin U.
Great, simple explanation, not to mention easily expandable if you wanted to explain constructors when dealing with inheritance/polymorphism.
Crag
+2  A: 

I'm not sure just how you are looking at this in school or what your teacher is telling you, but just a suggestion--do not use getters and setters as a matter of habit. There is a reason they are not "automatic" in Java, it's because they really shouldn't be there.

When you have an object, the idea is to ask the object to do something for you--in other words, you should provide methods that act on the variables for you rather than getting the variable and acting on it and putting it back.

There are a lot of times you can't avoid getters--but they should only be added when you are sure you need them, not as a matter of habit.

Your constructor is the BEST place to set all your member variables. In fact, if you make variables final (a very good habit to get into, makes the class "immutable") the constructor will be the only place you can assign them.

Your constructor is guaranteed to be called when your object is created, so it's a good place to set your object up. In your constructor if you ensure the object is in a valid state AND you have final member variables, it's impossible to get your object into an invalid state--this is a really handy practice and can save you a lot of debugging time.

Bill K