Neil Wilkinson wrote:If I want to create an instance of a class and throw something else in there before the class copy does it's thing, use a constructor.
I don't know what you mean with the "class copy". A class is like a blueprint for creating objects - the class is the "plan" that lays out what objects of that class look like.
For example, in the class you declare member variables and methods. Each object that you create from that class will have the member variables, and you can call the methods declared in the class on those objects.
Neil Wilkinson wrote:I'm a bit confused by the statements though, as I though these could also be added to a method within the class itself. do I really need to use a constructor to add statements?
I didn't say you needed a constructor to add statements to methods. You cannot add arbitrary statements in a class itself, outside a constructor or method.
Let's look at an example.
This class shows what a House object looks like. It has one member variable,
address. It has a constructor that allows you to pass the address when you create an object using class House. It also has a method to print the address. You can use this to create House objects:
Note how we can pass the address when we create the object. That address is passed to the constructor, which saves it in the
address member variable of the object you're creating. You need a constructor to be able to pass the address.
You cannot put arbitrary statements at class level. For example, this would result in a compiler error: