For Programmers: Free Programming Magazines  


Home > Archive > Java Help > May 2006 > Constructors









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Constructors
crazy.marc@gmail.com

2006-05-28, 4:06 am

Hi! I dont 100% understand Constructors (what they're use is/how they
are used) and I was just wondering if anybody could please take the
time to give me a bit of help on the subject.

Thanks very much,
Marc

Allan M. Bruce

2006-05-28, 4:06 am


<crazy.marc@gmail.com> wrote in message
news:1148797603.958229.97290@u72g2000cwu.googlegroups.com...
> Hi! I dont 100% understand Constructors (what they're use is/how they
> are used) and I was just wondering if anybody could please take the
> time to give me a bit of help on the subject.
>
> Thanks very much,
> Marc
>


A constructor is an initializer for the object you are making - it has the
same name as the object you are initializing. You can tell a constructor
from any other method as it has no return type. The reason for this is due
to the fact that when you make a new Object you want that object returned
therefore you cannot specify a return type. So if you see the following
singatures:

public void something1()
public something2()
public int something3()

You know immediately that (unless there is an error) something2() is a
constructor for an object called something2.

You can make different constructors using method overloading which means you
can initialize the object in several different ways, although you will only
need one per object created.

I suggest you also get yourself a good Java book, there are lots avialable
and some online too. They will describe the use of constructors better than
I can!
Allan


VisionSet

2006-05-28, 8:07 am


"Allan M. Bruce" <allanmb@TAKEAWAYdsl.pipex.com> wrote in message
news:B5ednYNOIJwczeTZRVny1Q@pipex.net...

> public void something1()
> public something2()
> public int something3()
>


<style police/>

public class Something2 {

public Something2() {}
}

Uppercase please for class names

--
Mike W


Paul Hamaker

2006-05-28, 8:07 am

http://javalessons.com/cgi-bin/fun/...ode=ctr&sub=fun
is a lesson/example concerning ctors.
--------------------
Paul Hamaker, SEMM, teaching ICT since 1987
http://javalessons.com

Allan M. Bruce

2006-05-28, 7:10 pm


"VisionSet" <spam@ntlworld.com> wrote in message
news:wVeeg.2824$l8.2517@newsfe6-win.ntli.net...
>
> "Allan M. Bruce" <allanmb@TAKEAWAYdsl.pipex.com> wrote in message
> news:B5ednYNOIJwczeTZRVny1Q@pipex.net...
>
>
> <style police/>
>
> public class Something2 {
>
> public Something2() {}
> }
>
> Uppercase please for class names
>


I wasnt going to go into uppercase for classes to avoid complication but
yes, the standard style is to use upper case for the first letter of classes
and therefore constructors, and lower case for the first letter of methods.
Allan


crazy.marc@gmail.com

2006-05-28, 7:10 pm

Thank you very much =)

when using constructors, does it always need to be assigned like:
--------------------------------
class test1 {
static int i_1, i_2;
test1(int i, int i2) {
i_1 = i;
i_2 = i2;
}
}
--------------------------------
or is there some way that an initialization list or something of the
likes can be done?

Thanks,
marc

Allan M. Bruce

2006-05-28, 7:10 pm


<crazy.marc@gmail.com> wrote in message
news:1148859677.657260.62910@i40g2000cwc.googlegroups.com...
> Thank you very much =)
>
> when using constructors, does it always need to be assigned like:
> --------------------------------
> class test1 {
> static int i_1, i_2;
> test1(int i, int i2) {
> i_1 = i;
> i_2 = i2;
> }
> }
> --------------------------------
> or is there some way that an initialization list or something of the
> likes can be done?
>
> Thanks,
> marc
>


I dont know what you mean. If you want to specify the value of 2 integers
(or similar) on construction of an object then there are two ways, 1) with a
constructor or 2) with an explicit initialise routine which you must call
separately. Perhaps if you word your question better, I can help.

Allan


crazy.marc@gmail.com

2006-05-28, 10:05 pm

okay uhm I asked this to a friend of mine, but he only knows C++ and he
understood, and I'll give the example here.
In C++ it would be done as the following :

class test1 {
static int i_1, i_2;
test1(int i, int i2): i_1(i), i_2(i2) { }
}

This means that it will automatically assign the value of i to i_1 and
i2 to i_2 without the code :

class test1 {
static int i_1, i_2;
test1(int i, int i2) {
i_1 = i;
i_2 = i2;
}
}

Is there a way to do this in java?

Hope its worded better

Marc

Bjorn Abelli

2006-05-29, 4:17 am


<crazy.marc@gmail.com> wrote...

> okay uhm I asked this to a friend of mine,
> but he only knows C++ and he
> understood, and I'll give the example here.
> In C++ it would be done as the following :
>
> class test1 {
> static int i_1, i_2;
> test1(int i, int i2): i_1(i), i_2(i2) { }
> }
>
> This means that it will automatically assign the value
> of i to i_1 and i2 to i_2 without the code :


"Automatically assign"? ;-)

IMHO, I can't see the benefit of doing it in a initialization list rather
than within the body of the ctor. Actually, I think the example below is
much more readable than the example above.

> class test1 {
> static int i_1, i_2;
> test1(int i, int i2) {
> i_1 = i;
> i_2 = i2;
> }
> }
>
> Is there a way to do this in java?


There's no initialization list for Java ctors, but IMHO that's an advantage,
not a divantage. :-)

The only benefit of initialization lists I see, would be to use it for calls
of other ctors. That needs to be done as the first line in the body, which
would be more clear if it was only done in an initialization list, but WTH,
writing it one line below isn't that hard either... ;-)

Roedy has a fairly good description on what you can or cannot do in ctors:

http://www.mindprod.com/jgloss/constructor.html

// Bjorn A



Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php
Oliver Wong

2006-05-29, 7:09 pm

<crazy.marc@gmail.com> wrote in message
news:1148869513.431927.81750@j55g2000cwa.googlegroups.com...
>
> class test1 {
> static int i_1, i_2;
> test1(int i, int i2) {
> i_1 = i;
> i_2 = i2;
> }
> }


BTW, it's probably a design problem if your constructor is using
parameters it receives to assign to *static* fields.

- Oliver

Joerg Simon

2006-05-29, 7:09 pm

Hello Bjorn && crazy.marc,

I normally don't to top posts, but I am not shure unter what part of
your post I should write that, so I top post ;).

I think the big difference here is, that in C++ you have
copy-constructors, and in java not, because everything is on the heap.

So a ctor in c++ with initialization lists has the advantage that you
directly call the ctor of the members, and no temp object has to be
made. However, if you do it in your body, you first reserv the place for
the vars, then call the copy-ctor and assign the vars.

In java you don't have a copy-ctor. However, normally you can't direktly
translate that code you did it. Using int was a bad example. Normally,
everything in Java is a call-by-reference for objects.

So you should condiser the following:

C++: [pseudo, it has been a few months since my last c++ proj. and c#
and java is just to different... @_@]
class Test1 {

A &a1_, &a2_;

// ctor
Test1(A &a1, A &a2): a1_(a1), a2_(a2) {}
}

Here you just copy references, so the difference to a variance with the
copying in the body in not very big. Actually, that is, what you do in java.

Lg,
Joerg Simon

(PS.: Execuse me for my english... @_@)

Bjorn Abelli schrieb:
> <crazy.marc@gmail.com> wrote...
>
>
> "Automatically assign"? ;-)
>
> IMHO, I can't see the benefit of doing it in a initialization list rather
> than within the body of the ctor. Actually, I think the example below is
> much more readable than the example above.
>
>
> There's no initialization list for Java ctors, but IMHO that's an advantage,
> not a divantage. :-)
>
> The only benefit of initialization lists I see, would be to use it for calls
> of other ctors. That needs to be done as the first line in the body, which
> would be more clear if it was only done in an initialization list, but WTH,
> writing it one line below isn't that hard either... ;-)
>
> Roedy has a fairly good description on what you can or cannot do in ctors:
>
> http://www.mindprod.com/jgloss/constructor.html
>
> // Bjorn A
>
>
>
> Inviato da X-Privat.Org - Registrazione gratuita http://www.x-privat.org/join.php

Andrew McDonagh

2006-05-29, 7:09 pm

Oliver Wong wrote:
> <crazy.marc@gmail.com> wrote in message
> news:1148869513.431927.81750@j55g2000cwa.googlegroups.com...
>
> BTW, it's probably a design problem if your constructor is using
> parameters it receives to assign to *static* fields.
>
> - Oliver


agreed - unless you are using the MonoState pattern.
Thomas Hawtin

2006-05-29, 7:09 pm

Joerg Simon wrote:
>
> I think the big difference here is, that in C++ you have
> copy-constructors, and in java not, because everything is on the heap.


And member variables are either reference (pointer) types or primitives.
And simple classes are often immutable (or should be immutable).

Still, there are times when you need explicit defensive copies in Java.
Receiving an array in a set method or returning one in a get method, for
instance. In C++ the copy tends to be implicit.

> C++: [pseudo, it has been a few months since my last c++ proj. and c#
> and java is just to different... @_@]
> class Test1 {
>
> A &a1_, &a2_;
>
> // ctor
> Test1(A &a1, A &a2): a1_(a1), a2_(a2) {}
> }

;
>
> Here you just copy references, so the difference to a variance with the
> copying in the body in not very big. Actually, that is, what you do in
> java.


In C++ you can't move reference initialisation into the constructor body:

class Test2 {
int &i;
Test2(int &x) {
i = x;
}
};

copyref.cpp: In constructor ‘Test2::Test2(int&)’:
copyref.cpp:3: error: uninitialized reference member ‘Test2::i’

The Java equivalent is:

class Test2 {
final int[] i;
Test2(final int[] x) {
i = x;
}
}

This works because of Java's concept of definitely
initialised/uninitialised. In C++ variables are initialised effectively
at point of declaration (possibly implicitly). In Java initialisation
may happen later, if at all.

Tom Hawtin
--
Unemployed English Java programmer
http://jroller.com/page/tackline/
crazy.marc@gmail.com

2006-05-29, 7:09 pm

Yes, that was my mistake when jotting this down =)

Marc

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com