Java problems with moving class objects into an array. Not sure if there is a fault with the constructors or the array declaration
Having a bit of trouble moving objects I've created into an array. So what I actually need to do is create objects than move them into an array. I'm not quite sure what I'm doing wrong.
If you think I should be using a arraylist instead of a array please say so
I made a quick example with less data fields than my actual program but its pretty well the same as my lar开发者_开发问答ger problem. Thank you for your time.
    public class Music {
private static String songTitle;
private static double songLength;
private int rating;
    public Music(String songTitle, double songLength, int rating) {
    // TODO Auto-generated constructor stub
    }
    public static String getsongTitle()
    {
        return songTitle;
    }
    public static double getsongLength()
     {
    return songLength;
    }
     public static   int rating()
    {
      return rating();
     }
     //constructors for music objects
     Music song1 = new Music ("song name", 5.32, 10);
     Music song2 = new Music ("billy",1.2, 8 );
     Music song3 = new Music ("hello", 1.5, 9 );
     static        //Create array and make posistion 0 = song1
     Music[] songDetails = new Music[3];{
      songDetails[0] = song1;
        }
       public static void main(String[] args) {
       //print first place in array
        System.out.println(songDetails[0]);
       }
Edit had a spelling mistake in the code and was missing the word static in the array declaration
Almost everything is OK here ;) There are many ways. Simply access songDetails from nonstatic method (and change Music fields to nonstatic, and implement Music constructor):
public class TryMusic {      
    Music song1 = new Music("song name", 5.32, 10);
    Music song2 = new Music("billy", 1.2, 8);
    Music song3 = new Music("hello", 1.5, 9);
    //Create array and make posistion 0 = song1
    Music[] songDetails;// = new Music[3];
    {
       //some initializations goes here
       //...
       //create array when you know how many songs you have
       songDetails = new Music[3];
       // and now fill the array. Possibly iteration? if no then simply use {} syntax
        songDetails[0] = song1;
        songDetails[1] = song2;
        songDetails[2] = song3;
    }
    public void go() {
        //print first place in array
        System.out.println(songDetails[0]);
    }
    public static void main(String[] args) {
        new TryMusic().go();
    }
}
You may also want do it like this:
public class TryMusic {
    //Create array and make posistion 0 = song1
    Music[] songDetails;// = new Music[3];
    {
        //if you still need initialization block, but don't need field for each song
        songDetails = new Music[3];
        //possibly iteration? if no then simply use {} syntax instead
        songDetails[0] = new Music("song name", 5.32, 10);
        songDetails[1] = new Music("billy", 1.2, 8);
        songDetails[2] = new Music("hello", 1.5, 9);
    }
    public void go() {
        //print first place in array
        System.out.println(songDetails[0]);
    }
    public static void main(String[] args) {
        new TryMusic().go();
    }
}
or this (more readable):
public class TryMusic {
    //Create array and make posistion 0 = song1
    //if simply array is enough
    Music[] songDetails = { new Music("song name", 5.32, 10),
         new Music("billy", 1.2, 8),
        new Music("hello", 1.5, 9)
    };
    public void go() {
        //print first place in array
        System.out.println(songDetails[0]);
    }
    public static void main(String[] args) {
        new TryMusic().go();
    }
}
or (more flexible):
public class TryMusic {
    //Create array and make posistion 0 = song1
    List<Music> songDetails = new ArrayList(){{
       add(new Music("song name", 5.32, 10));
       add(new Music("song name", 5.32, 10));
       add(new Music("hello", 1.5, 9));
    }};
    public void go() {
        //print first place in array
        System.out.println(songDetails.get(0));
    }
    public static void main(String[] args) {
        new TryMusic().go();
    }
}
COMMENT FOR YOUR EDIT: the array was static, but initialization was not static. You missed static keyword before initialization block:
public class TryMusic {
//Create array and make posistion 0 = song1
static Music[] songDetails = new Music[3];
static {
    songDetails[0] = new Music("song name", 5.32, 10);
    songDetails[1] = new Music("billy", 1.2, 8);
    songDetails[2] = new Music("hello", 1.5, 9);
}
public static void main(String[] args) {
    System.out.println(songDetails[0]);
}
}
and static version with list:
    public class TryMusic {
    //Create array and make posistion 0 = song1
    static List<Music> songDetails = new ArrayList(){{
       add(new Music("song name", 5.32, 10));
       add(new Music("song name", 5.32, 10));
       add(new Music("hello", 1.5, 9));
    }};
    public static void main(String[] args) {
        System.out.println(songDetails.get(0));
    }
}
And oh, I see it now, your Music class is broken too. Use instance fields instead of static and assign values in constructor:
public class Music {
    private String songTitle;
    private double songLength;
    private int rating;
    public Music(String songTitle, double songLength, int rating) {
        this.songTitle = songTitle;
        this.songLength = songLength;
        this.rating = rating;
    }
    public String getsongTitle() {
        return songTitle;
    }
    public double getsongLength() {
        return songLength;
    }
    public int rating() {
        return rating;
    }
    @Override
    public String toString() {
        return "Music{" + "songTitle=" + songTitle + ", songLength=" + songLength + ", rating=" + rating + '}';
    }
}
A very simple way to proceed is:
 //constructors for music objects
 static Music song1 = new Music ("song name", 5.32, 10);
 static Music song2 = new Music ("billy",1.2, 8 );
 static Music song3 = new Music ("hello", 1.5, 9 );
 static Music[] songDetails = { song1, song2, song3 };
 ...
 public static void main(String[] args) {
    System.out.println(songDetails[0].getsongTitle());
 }
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论