JAVA default constructors
Results 1 to 4 of 4

Thread: JAVA default constructors

  1. #1
    Junior Member
    Join Date
    Aug 2002
    Posts
    19

    JAVA default constructors

    This isn't making much sense to me. I don't have any trouble with the language and making a program that works, but the terminology keeps throwing me off. Can someone tell me what a default constructor is, and what it's purpose is? And if possible, write some code to show me what exactly it is, because I'm just not getting it.

    My teacher, who has been VERY LITTLE HELP to me, said it's just to initialize values of certain variables. If this is true, why not just initialize them when you declare the variable? That is to say, why not do

    double number1 = 100;

    rather than,

    double number1;

    public default()
    {
    number1 = 100;
    }

    I hope my question was clear enough. I'm so confused right now I don't even know if I can ask question correctly. :-\

    Thanks,
    Ramzi

  2. #2
    Elite Hacker
    Join Date
    Mar 2003
    Posts
    1,407
    The default constructor is kindof in the background. It is there even if you don't write it. Let's say you have this
    Code:
    public class Hello
    {
        int int1,int2;
    
        public int getValue()
        {
             return int1;
        }
    }
    Basically the default constructor would look like this
    Code:
    public Hello()
    {
         int1 = 0;
         int2 = 0;
    }
    So that way you would get 0 if you called the getValue() method from another class. The default constructor is there so that you don't have to initialize the values. If you want to initialize them you make your own constructor that initializes them to whatever value you want.
    Hope this cleared things up.

  3. #3
    Senior Member
    Join Date
    Sep 2003
    Posts
    179
    h3r3tic's answer is good. I would give him points but I'm told I need to spread them around.

    Anyway, a little more info on the default constructor. Any constructor takes the definition of a class and creates an instence, (or an object) of that class. The constructor defines how this object is created. A default constructor is created when NO OTHER constructor is created.

    The defult constructor takes no arguments, it builds the object, but does not assign values, or do any special tasks while the object is being created.

    If a constructor is defined, that lets say takes one argument then no other constructors are created. If you try to use a constructor with no arguments, you will get a compile error.

    You can also create multiple constructors, there signature must be difffrent though. Example you can have a Date() constructor that takes one argument, one that takes two arguments, or one that takes three arguments.

    Date(int day) or Date(int day, int month) or Date(int day, int month, int year)

    This is the base of polymorphism (the ability of the program to run diffrently based on runtime conditions)

    Lets say the user of your program wants to make a Date object, he can either enter just a day, a day and month, or day, month, and year. Your program will then choose the proper constructor to use to create the opropreate Date object. But notice that if you can not create a date objet with no arguments, cause that constructor has not been defined.

    The default constructor is provided by the compiler when no other constructors are defined, but if you define even one constructor, then the compiler does not create a default constructor, it is up to you to create all needed constructors.

    Hope I helped and didn't just confuse you more.

    DeafLamb

  4. #4
    Senior Member
    Join Date
    May 2003
    Posts
    226
    For example you got a Clock class. Everytime you create a new clock i.e
    Code:
    Clock clock = new Clock();
    You want to set the default time for the clock time to 00:00. You can intialize the variables in the default constructors
    Code:
    public Clock()
    {
        int hour = 0;
        int minute = 0;
    }
    You can also set the time of the Clock class by passing the time in
    Code:
    public Clock(int h, int m)
    {
        hour = h; 
        minute = m;
    }
    
    Clock clock = new Clock(5, 20);
    It's normally use to intialize the data of the class or sets the data of a class. For example in the case of inheritance super() is use to called the superclass constructors

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •