November 5th, 2003, 01:53 AM
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;
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. :-\
November 5th, 2003, 01:58 AM
The default constructor is kindof in the background. It is there even if you don't write it. Let's say you have this
Basically the default constructor would look like this
public class Hello
public int getValue()
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.
int1 = 0;
int2 = 0;
Hope this cleared things up.
November 5th, 2003, 04:31 AM
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.
November 5th, 2003, 05:58 AM
For example you got a Clock class. Everytime you create a new clock i.e
You want to set the default time for the clock time to 00:00. You can intialize the variables in the default constructors
Clock clock = new Clock();
You can also set the time of the Clock class by passing the time in
int hour = 0;
int minute = 0;
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
public Clock(int h, int m)
hour = h;
minute = m;
Clock clock = new Clock(5, 20);