ggFTW

MMORPG Gamer Community


Go Back   ggFTW Forum > Off Topic > Computers & Technology

Perfect World
Looking for a new MMO?

Try 

Perfect World

Rating:  

7.4

 
LinkBack Thread Tools Display Modes
02-09-2009   #1 (permalink)
I am a girl. =)
Special
Shugi's Avatar
Games
AuditionCounter-Strike OnlineGunboundHabbo Hotel
Awards MoM AwardTwitter Fan
 
Join Date: Dec 2008
Location: Behind you
Posts: 3,253
iTrader: (1)
Shugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond repute
Send a message via AIM to Shugi Send a message via Skype™ to Shugi
IGN: Penor
Xfire: shugix
Default 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.
__________________
+Rep me if u think i be ballin, u no wat i sayin?

Steam: Shugi | Battle.net: Shugi#1454 | LoL.NA: xShugi | GW2: xShugi.3601
 
Get rid of this ad by registering for our community.
02-09-2009   #2 (permalink)
/gg FTW! Moogler
Dragonfly's Avatar
 
Join Date: Jan 2008
Location: Near a lilly pad
Posts: 142
iTrader: (0)
Dragonfly is on a distinguished road
Send a message via AIM to Dragonfly
IGN: Dragonfly
Class: Tia
Level: 67
Guild: Fusion
Default

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
__________________
Dragonfly - Level 6x Tia Sig Made by Synlight

Last edited by Dragonfly; 02-09-2009 at 05:05 PM.
 
02-09-2009   #3 (permalink)
ggFTW Stalker
SilentSaber's Avatar
 
Join Date: Oct 2008
Location: 自分の幻想
Posts: 1,401
iTrader: (0)
SilentSaber has a spectacular aura aboutSilentSaber has a spectacular aura aboutSilentSaber has a spectacular aura about
Class: MME
Level: -2147483647
Guild: YetAnotherUC
Default

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.
__________________
Human history is the story of complaisance. While disaster is fresh in our memory, we take precautions. But as the memory of disaster recedes, we start to take risks.
 
02-09-2009   #4 (permalink)
I am a girl. =)
Special
Shugi's Avatar
Games
AuditionCounter-Strike OnlineGunboundHabbo Hotel
Awards MoM AwardTwitter Fan
 
Join Date: Dec 2008
Location: Behind you
Posts: 3,253
iTrader: (1)
Shugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond repute
Send a message via AIM to Shugi Send a message via Skype™ to Shugi
IGN: Penor
Xfire: shugix
Default

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. =/
__________________
+Rep me if u think i be ballin, u no wat i sayin?

Steam: Shugi | Battle.net: Shugi#1454 | LoL.NA: xShugi | GW2: xShugi.3601
 
02-09-2009   #5 (permalink)
Let's Pie Creation!
Yukipyon's Avatar
Games
MapleStoryS4 LeagueEden Eternal
 
Join Date: Nov 2008
Location: Beyond that hill
Posts: 727
iTrader: (0)
Yukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to behold
IGN: Aokawa
Default

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. >.<
__________________
"...And just like that, he became the king of the world."
 
02-09-2009   #6 (permalink)
I am a girl. =)
Special
Shugi's Avatar
Games
AuditionCounter-Strike OnlineGunboundHabbo Hotel
Awards MoM AwardTwitter Fan
 
Join Date: Dec 2008
Location: Behind you
Posts: 3,253
iTrader: (1)
Shugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond reputeShugi has a reputation beyond repute
Send a message via AIM to Shugi Send a message via Skype™ to Shugi
IGN: Penor
Xfire: shugix
Default

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
__________________
+Rep me if u think i be ballin, u no wat i sayin?

Steam: Shugi | Battle.net: Shugi#1454 | LoL.NA: xShugi | GW2: xShugi.3601
 
02-09-2009   #7 (permalink)
Let's Pie Creation!
Yukipyon's Avatar
Games
MapleStoryS4 LeagueEden Eternal
 
Join Date: Nov 2008
Location: Beyond that hill
Posts: 727
iTrader: (0)
Yukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to beholdYukipyon is a splendid one to behold
IGN: Aokawa
Default

Glad to help. ^.^
__________________
"...And just like that, he became the king of the world."
 
02-11-2009   #8 (permalink)
ggFTW Stalker
SilentSaber's Avatar
 
Join Date: Oct 2008
Location: 自分の幻想
Posts: 1,401
iTrader: (0)
SilentSaber has a spectacular aura aboutSilentSaber has a spectacular aura aboutSilentSaber has a spectacular aura about
Class: MME
Level: -2147483647
Guild: YetAnotherUC
Default

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.
__________________
Human history is the story of complaisance. While disaster is fresh in our memory, we take precautions. But as the memory of disaster recedes, we start to take risks.
 

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Battlestar Galactica Online
Need a new browser game?

Try 

Battlestar Galactica Online

Rating:  

3.5
Hide this banner by registering for our community.