Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Help on Constructors
Index -> Programming, Java -> Java Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Client77




PostPosted: Tue Mar 29, 2005 4:03 pm   Post subject: Help on Constructors

I'm currently taking a programming class and this came up today. I was wondering what a constructor is? What is its purpose and Where are they located in a program?

Thanks for any help in advance.
Sponsor
Sponsor
Sponsor
sponsor
wtd




PostPosted: Tue Mar 29, 2005 6:06 pm   Post subject: (No subject)

A constructor sets up the initial state of an object.

Let's say I have the simply Name class I like to trot out. A Name object's state is represented by two variables: the first and last names.

code:
class Name
{
   private String first;
   private String last;
}


Now, a constructor for this class would give some initial value to these variables. For instance:

code:
class Name
{
   private String first;
   private String last;

   public Name(String initFirst, String initLast)
   {
      first = initFirst;
      last = initLast;
   }
}


So that now I can use the constructor like so:

code:
Name bobsName = new Name("Bob", "Smith");
Client77




PostPosted: Tue Mar 29, 2005 9:11 pm   Post subject: (No subject)

Wow thats a lot of jargon, would it be possible for you to simply that a little more. Thanks a lot.
wtd




PostPosted: Wed Mar 30, 2005 12:03 am   Post subject: (No subject)

Honestly, no, I don't think I can simplify it anymore than that. A constructor simply constructs the initial state of an object.
Martin




PostPosted: Wed Mar 30, 2005 1:47 pm   Post subject: (No subject)

An object is a container.

Let's say that we have a Box class, that stores information about a Box. Now, our box has three properties: length, width and height.

If you were a company that made Boxes, and I called you and asked you to make me a box, you would have to know what size of a box I wanted. That's what a constructor is for.

Let's suppose that the box class was defined. I could say

code:
Box myBox = new Box();


Here I am asking for a Box. However, we really have no idea how big the Box is. Now, to deal with this we have a few choices: we could say that all boxes are 1 x 1 x 1 if nobody specifies how big they are, or we could make sure the user gives dimensions.

code:
Box myBox = new Box (5, 3, 2);


The above would create a box, and set its dimensions to be 5 x 3 x 2.
Client77




PostPosted: Wed Mar 30, 2005 5:07 pm   Post subject: (No subject)

So what I understand then is that it declares or contructs the state of an object using defaults and you can also add your own values or something like that and I guess I would only need to use this at the beginning of a program.
Right?
wtd




PostPosted: Wed Mar 30, 2005 6:46 pm   Post subject: (No subject)

Client77 wrote:
So what I understand then is that it declares or contructs the state of an object using defaults and you can also add your own values or something like that and I guess I would only need to use this at the beginning of a program.
Right?


It merely assigns values to instance variables. Tjose variables are declared in the class scope.

code:
class Foo
{
   private int bar; // instance variable declaration

   public Foo()
   {
      bar = 42; // instance var initialization.
   }
}
Martin




PostPosted: Wed Mar 30, 2005 10:00 pm   Post subject: (No subject)

Not at the beginning of the program, whenever you are creating an object.

Using my box example, here's how it works:

1. We create the class, which is a template for a box. It tells us what a box is, but it isn't actually a box. Think of it like the blueprint for a box. There is no box. Wink
2. We want an object, so, to create an object, we call the box's constructor. Note that the constructor has the same name as the actual class. The line
code:
Box myBox = new Box(2,3,4);
creates a new Box. Now we have brought the box into existence. Think of this like calling the box store and asking for a box. They ask you how big you want the box, and you tell them 2 x 3 x 4.
3. Now you have your box, and it's called myBox. It exists. You can do stuff with it. Supposing you have created a function within the Box class called Open, you could say
code:
myBox.Open();


Below is some sample code:
code:
class Box
{
    private int length;
    private int width;
    private int height;

    public Box ()
    {
        length = 1;
        width = 1;
        height = 1;
    }
    public Box (const int l, const int w, const int h)
    {
        length = l;
        width = w;
        height = h;
    }
    public void Open ()
    {
        System.out.println ("You opened the box!");
    }
}


If you don't understand the idea of objects, well, it would probably be a lot more helpful to get your teacher to explain it to you after school one day or something.
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Tue May 03, 2005 7:54 pm   Post subject: (No subject)

*Is starting to learn Java now*

Martin: You've got two Box methods. I assume that Java can tell the difference between them because of the differences in the parameters. The code you've got there would mean that if the code that creates the new box had no parameters, it would use 1 x 1 x 1. And if it had parameters, it'd use what is entered. Am I right so far? What if you only enter width and height?
Hikaru79




PostPosted: Tue May 03, 2005 8:35 pm   Post subject: (No subject)

Cervantes wrote:
*Is starting to learn Java now*

Martin: You've got two Box methods. I assume that Java can tell the difference between them because of the differences in the parameters. The code you've got there would mean that if the code that creates the new box had no parameters, it would use 1 x 1 x 1. And if it had parameters, it'd use what is entered. Am I right so far? What if you only enter width and height?


Cervantes, I'm not Martin, but I can answer your questions ^_^
Yes, providing multiple constructors with the same method name but different parameters works the way you described. This is called "overloading" and it's a really useful ability. And it doesn't just apply to constructors -- any method at all can be overloaded.
If you had only entered width and height, that would only be two parametrs, not 3 or 0. So, it would give an error -- there is no such constructor!
Cervantes




PostPosted: Wed May 04, 2005 8:45 am   Post subject: (No subject)

Right, I understand that it wouldn't work. What I was asking was, is there a way to make the program work such that if width and height were entered, and not length, the program would use a default value for length. The restriction to this question is that I don't want to have to make another method. So basically, is there a way to make default parameters, apart from making lots and lots of methods by the same name?
wtd




PostPosted: Wed May 04, 2005 12:41 pm   Post subject: (No subject)

I don't believe that's possible in Java, though it is in C++. The problem is ambiguity. If I call:

Java:
foo.bar(42, 34);


Does it refer to:

Java:
public void bar(int a, int b)


Or:

Java:
public void bar(int a, int b, String c = "hello")


?
rizzix




PostPosted: Wed May 04, 2005 7:06 pm   Post subject: (No subject)

Java:
foo.bar(42, 34);


A solution is this... (and this is how you are supposed to do it in java anyways)
Java:
public void bar(int a, int b) {
    this.bar(a,b,"blah");
}

Java:
public void bar(int a, int b, String c) {
    this.a = a; this.b = b; this.c = c;
}
Display posts from previous:   
   Index -> Programming, Java -> Java Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 13 Posts ]
Jump to:   


Style:  
Search: