aspose file tools*
The moose likes Beginning Java and the fly likes Confused...creating an ojbect and adding it Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Confused...creating an ojbect and adding it" Watch "Confused...creating an ojbect and adding it" New topic
Author

Confused...creating an ojbect and adding it

Shaggy Rogers
Greenhorn

Joined: Mar 10, 2006
Posts: 26
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

Joined: Feb 07, 2005
Posts: 2367
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

Joined: Sep 16, 2005
Posts: 1780
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.


There is no emoticon for what I am feeling!
Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 95
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

Joined: Sep 16, 2005
Posts: 1780
I think Keith wanted Shaggy to figure out the syntax hisself
Tom Fulton
Ranch Hand

Joined: Mar 30, 2006
Posts: 95
Ah...I apologize, that makes sense.

I'm just getting used to the etiquette here.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused...creating an ojbect and adding it