ggFTW

ggFTW (http://ggftw.com/forum/)
-   Computers & Technology (http://ggftw.com/forum/computers-technology/)
-   -   Some Java help (http://ggftw.com/forum/computers-technology/38436-some-java-help.html)

Shugi 02-09-2009 02:40 PM

Some Java help
 
I have some problems understanding overloaded constructors...

So I created 2 constructors which are the following:
Code:

        //Contructor
        public Rectangle() {
                storedLength = 1;
                storedWidth = 1;
        }
       
        //Constructor
        public Rectangle(double length, double width) {
                storedLength = length;
                storedWidth = width;
        }

I know that the first method will set the default values, but I don't really understand how to incorporate the second one into my client code. =/ Any ideas or explanation of constructors would be appreciated.

Dragonfly 02-09-2009 04:58 PM

So basically that is in a class. When you instantiate this class in a main class or something, for example:

Rectangle box = new Rectangle();

The first half of this code will make a class named box with the methods of Rectangle. The part after the equal sign will call upon that first method, setting the storedLength and storedWidth to 1.

But what if you want it to start off with other numbers other than 1? This is what the other constructor is for. If you want to make the default values different than 1, then you can use the following:

Rectangle box = new Rectangle(61.22, 21);

This will give this box to have a storedLength of 61.22 and storedWidth of 21.

It is good to have many constructors if there are many different scenarios you want to start off on. It is called an overloaded constructor because there are several constructors with the same name but have different parameters. Although to humans the constructors' name look alike, to a computer it looks like the two constructors are totally different. The method name is not just the name itself but the parameters as well. So it is legal to code the following:

Quote:

public Rectangle() {
storedLength = 1;
storedWidth = 1;
}

public Rectangle(double length, double width) {
storedLength = length;
storedWidth = width;
}

public Rectangle(double length, double width, String name) {
storedLength = length;
storedWidth = width;
}

public Rectangle(double length, String name, double width) {
storedLength = length;
storedWidth = width;
}

public Rectangle(double length, int width) {
storedLength = length;
storedWidth = width;
}
Even though the names are the same, the compiler will look at the parameter as well. Even the number of parameters can be the same if the primitive types are different. Even the order of the primitive types matter. Depending on what you put in the parameter when you instantiate this class will determine which method will execute.

Hope this helps. If you have any other questions, ask away. I love java. :3

SilentSaber 02-09-2009 05:10 PM

In a simple sense, that allows you to either, when you create your object, to supply no arguments, or supply 2 doubles, the height and the width. No extra code to "incorporate" them into your main method.

Shugi 02-09-2009 06:37 PM

Oh thanks for the help.

But what if I'm using a method that assigns the value anyway? Wouldn't that make those methods obsolete?
For example
Code:

public void setLength(double newLength){
storedLength = newLength;
}

This is my dilemma really. I can't figure out how to incorporate both of the methods. =/

Yukipyon 02-09-2009 06:41 PM

Well, it's good practice to let your constructor method assign values to your instance variables. You should use "set" methods if you want to change the values of your instance variables later on in the program, or if your constructor doesn't assign values to your instance variables.

So, "set" methods are mainly used when you want to be able to change the values of an object's instance variables. But, if you don't plan to change the values of your instance variables after you initialize them, then you don't really need to use "set" methods; just let the constructor do the work. ^.^;


Sorry, if my explanation isn't very good. >.<

Shugi 02-09-2009 07:13 PM

Oh that makes a lot more sense! Thanks for your help. Haha My teacher is the stereotypical programmer, he is bad at explaining these things. =3

Yukipyon 02-09-2009 07:18 PM

Glad to help. ^.^

SilentSaber 02-11-2009 10:38 PM

Set methods are just to make your programs more efficient, otherwise you'd need to create a new object every time something changes. You'll use them more when you wright longer programs.


All times are GMT -7. The time now is 07:03 PM.

Powered by vBulletin® Version 3.8.2
Copyright ©2000 - 2016, Jelsoft Enterprises Ltd.