Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Confused...creating an ojbect and adding it

 
Shaggy Rogers
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's the deal, in my program there is a song class, which creates a Song object. However, part of my program requires me to create a song object from within another class. The code looks like:

//This method creates a song object and adds it to the list
public void enterSong(String title, String artist, int songLength)
{
Song newSong;
newSong = //??!?!

songList.add(newSong);

}

the song constructor looks like this:

public Song(String title, String artist, int songLength)
{
// initialise instance variables
this.title = title;
this.artist = artist;
playingTime = songLength;
timesPlayed = 0;
}

I am not sure how to create a new object here because of the parameters in the enterSong() method. I tried doing Song.Song() and that didnt' seem to work. Thanks for any help.
 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To create the new object you use the syntax

new Song(parameter list)

where you send in parameters that match in position and type with the parameter list in at least one constructor of Song.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shaggy Rogers:
I tried doing Song.Song() and that didnt' seem to work.


Sounds like you got the Song.Song blues -- every garden grows one.
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Keith is correct...here is what your code would look like implementing his approach:

public void enterSong(String title, String artist, int songLength)
{
Song newSong;
newSong = new Song(title, artist, songLength);

songList.add(newSong);

}

the song constructor looks like this:

public Song(String title, String artist, int songLength)
{
// initialise instance variables
this.title = title;
this.artist = artist;
playingTime = songLength;
timesPlayed = 0;
}


The thing to remember is this: the variable you have called "newSong" is a reference (which equates to a pointer, containing an address). When you first declare it, the reference isn't pointing at anything. You need to instantiate an object for that reference to point at, and this is typically done with the "new" keyword, which calls the constructor for that class.
 
Jeff Albertson
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think Keith wanted Shaggy to figure out the syntax hisself
 
Tom Fulton
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah...I apologize, that makes sense.

I'm just getting used to the etiquette here.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic