Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

java newbie

1 view
Skip to first unread message

usaims

unread,
Sep 6, 2010, 4:14:04 PM9/6/10
to
Hi,

I'm taking Java at school, I've been instructed to create a rectangle
object and make a copy of it with this contructor:

public Rectangle(Rectangle r)

I search the API and it doesn't have examples on how to use this
constructor. Will somebody give me an example on how to use this
constructor? Below is my example in my program:

Rectangle rect_one = new Rectangle(0, 0, 300, 400);

John B. Matthews

unread,
Sep 6, 2010, 4:48:46 PM9/6/10
to
In article
<2ff8b113-734c-4cc6...@g17g2000yqe.googlegroups.com>,
usaims <usa...@yahoo.com> wrote:

If you mean java.awt.Rectangle,

<http://download.oracle.com/javase/6/docs/api/java/awt/Rectangle.html>

you'll find the copy constructor you want listed among those under
"Constructor Summary". You use the constructor just like the one you've
shown, only you feed it an existing Rectangle instead of four numbers.

Rectangle rect_two = new Rectangle(rect_one);

--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>

Nene

unread,
Sep 6, 2010, 5:36:10 PM9/6/10
to
On Sep 6, 4:48 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> In article
> <2ff8b113-734c-4cc6-921e-cb62a5847...@g17g2000yqe.googlegroups.com>,

Ok, thanks. The API documentation said this:
public Rectangle(Rectangle r)

If I knew this was just a normal java construction with the 'new' in
the line and ends with a semicolon I probably would have figured it
out. But the API documentation started this contructor with 'public'
which kind of threw me off.
figured it out.

markspace

unread,
Sep 6, 2010, 5:45:08 PM9/6/10
to
On 9/6/2010 1:14 PM, usaims wrote:

> I'm taking Java at school, I've been instructed to create a rectangle
> object and make a copy of it with this contructor:
>
> public Rectangle(Rectangle r)


We could, but I think this is a time where you should ask your
instructor. Getting stuck on a problem like this indicates some things
may have be omitted by your instructor during his/her lecture.

The answer is dead simple, btw, try to think about it. At least, write
some code even if it doesn't work. Try to then work the code around so
it at least might work.


Arved Sandstrom

unread,
Sep 6, 2010, 6:18:09 PM9/6/10
to

I agree with markspace on all counts. As a minor hint, I might add that a
copy constructor in Java has absolutely nothing special about it.

AHS
--
Before a man speaks it is always safe to assume that he is a fool.
After he speaks, it is seldom necessary to assume it. -- H.L. Mencken


Lew

unread,
Sep 6, 2010, 7:20:35 PM9/6/10
to
Nene wrote:
> Ok, thanks. The API documentation said this:
> public Rectangle(Rectangle r)
>
> If I knew this was just a normal java [sic] construction with the 'new' in

> the line and ends with a semicolon I probably would have figured it
> out. But the API documentation started this contructor with 'public'
> which kind of threw me off.
> figured it out.

<http://download.oracle.com/javase/tutorial/java/javaOO/accesscontrol.html>
explains about 'public', 'private', 'protected' and so-called "default" or
"package-private" (no keyword used) access restrictions.

The Javadocs show 'public' to indicate the access level of the constructor,
method or variable. One learns to use 'new' and the semicolon by reading the
tutorials, among other ways.

<http://download.oracle.com/javase/tutorial/index.html>

--
Lew

John B. Matthews

unread,
Sep 6, 2010, 8:57:37 PM9/6/10
to
In article
<a6dac51d-8ea6-4df3...@t20g2000yqa.googlegroups.com>,
Nene <rodb...@gmail.com> wrote:

> On Sep 6, 4:48 pm, "John B. Matthews" <nos...@nospam.invalid> wrote:
> > In article
> > <2ff8b113-734c-4cc6-921e-cb62a5847...@g17g2000yqe.googlegroups.com>,

[...]


> >
> > > Rectangle rect_one = new Rectangle(0, 0, 300, 400);
> >
> > If you mean java.awt.Rectangle,
> >
> > <http://download.oracle.com/javase/6/docs/api/java/awt/Rectangle.html>
> >
> > you'll find the copy constructor you want listed among those under
> > "Constructor Summary". You use the constructor just like the one
> > you've shown, only you feed it an existing Rectangle instead of
> > four numbers.
> >
> > Rectangle rect_two = new Rectangle(rect_one);
> >

[...]


>
> Ok, thanks. The API documentation said this:
> public Rectangle(Rectangle r)
>
> If I knew this was just a normal java construction with the 'new' in
> the line and ends with a semicolon I probably would have figured it
> out. But the API documentation started this contructor with 'public'
> which kind of threw me off. figured it out.

Excellent. I spoiled this one, but that leaves five others too assay
using the approach suggested by markspace and AHS. Also, the access
control table suggested by Lew is handy; not everything there made
complete sense to me initially, and I've returned to it more that once.

Arne Vajhøj

unread,
Sep 19, 2010, 7:20:40 PM9/19/10
to
On 06-09-2010 18:18, Arved Sandstrom wrote:
> markspace wrote:
>> On 9/6/2010 1:14 PM, usaims wrote:
>>> I'm taking Java at school, I've been instructed to create a rectangle
>>> object and make a copy of it with this contructor:
>>>
>>> public Rectangle(Rectangle r)
>>
>> We could, but I think this is a time where you should ask your
>> instructor. Getting stuck on a problem like this indicates some
>> things may have be omitted by your instructor during his/her lecture.
>>
>> The answer is dead simple, btw, try to think about it. At least,
>> write some code even if it doesn't work. Try to then work the code
>> around so it at least might work.
>
> I agree with markspace on all counts. As a minor hint, I might add that a
> copy constructor in Java has absolutely nothing special about it.

It is not that special in C++ either. The call is not necessarily
explicit as in Java, but C++ operator overload makes that a common
thing in C++.

The biggest differences is that it is not needed in the same
way as in C++ because of Java ref's and GC.

Arne

0 new messages