Now, the first thing i've noticed is that the constructor methods DO NOT return a value - which is what i'd expect for a constructor method. Thing is, if they don't return a value, then how does the program work??? What I mean is, each constructor method receives a set of parameters. And then what??? Looking at the code, it would appear that without returning anything, nothing else can hapen. Could somebody please enlighten me.
Cheers in advance, folks.
John Bonham was stronger, but Keith Moon was faster.
constructors set parameters for the object you are creating. What constructor is used depends on the number and type of parameters used when the object is created. look at the line
this has 4 parameters, which makes a call to this constructor
Now you have a rectangle object (rect) with the 4 corners set to 25, 25, 50, 50. the next line calls the printRect() method on using that object. then a few lines down you have
which makes a call to the constructor with 2 parameters
...then you call the printRect() method on that object again. so the constructors do not return a value, but instead set parameters when an object is created. (please correct me if i am wrong or if i use the wrong terminology...i'm still learning myself) [ May 08, 2003: Message edited by: jim gotti ]
Yes, constructors in Java don't return vales. Ever. Typically, you instantiate (create) an object in Java like this:
Your object is now instantiated. Typically, if you wanted that object to actually do something useful (like return a value or calculate a value or spawn a thread or whatever) you would call a method of that object.
Does this answer your question? You seem to have a non-Java coding background. Just out of curiosity, what language are you most familiar with?
In addition to what Jim wrote: Steve, notice that Jim did not use the word "method" with constructor. That's because they are not strictly methods because they have no return value. A constructor instantiates ( "makes" ) an object using its class as a "template". Constructors are normally used together with the new keyword. So Myclass mc = new MyClass(); can be read as "make me a new object using the class MyClass as a template, and put a handle (a reference) to the newly created object in the variable mc which is of type: reference to MyClass". By having constructors with parameters you can instantiate objects whose "insides" are different to other objects of the same kind (class). If still uncertain, please keep asking... -Barry PS Hi Tom, didn't see you there - but the more the merrier [ May 08, 2003: Message edited by: Barry Gaunt ]
Steve, are you receiving this? Please give us feedback. [ May 10, 2003: Message edited by: Barry Gaunt ]
Joined: Sep 23, 2002
Err, yeah, I've sort of understod it, thanks. But this has lead me to a follow-up question - just why should we bother with constructors at all then. The cattledrive assignments (for example), that i've completed - have worked fine without me needing to use(??) constructors. What I mean is, if constructors don't actually do anything - why use them?? :roll:
Joined: Jul 02, 2002
Well, they do do something. they initialize a new object...set its parameters. for example. take the Vector API if you just make a new constructor. Vector v = new Vector(); //notice no parameters this calls the default constructor, noted below from the API.
Constructor Summary Vector() Constructs an empty vector so that its internal data array has size 10 and its standard capacity increment is zero. However if you create a new Vector with the following: Vector v = new Vector(50); it will call the constructor that takes an int as its parameters, noted below.... Vector(int initialCapacity) Constructs an empty vector with the specified initial capacity and with its capacity increment equal to zero. ....and so on. So see? the constructor do actually do something...just sometimes when people make a new object, such as vectors, linked lists, arrays...they make them with no parameters and Java notices and it just makes a call to the default constructor. Also, if you write code , you do not need to define a constructor, java will use its default constructor...however if you define ONE constructor, then you have to take total control of them all(if more than one). This is why you have never had to define the constructors when you do the cattledrive examples. [ May 11, 2003: Message edited by: jim gotti ]
I’ve looked at a lot of different solutions, and in my humble opinion Aspose is the way to go. Here’s the link: http://aspose.com