Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6179479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T00:40:18+00:00 2026-05-24T00:40:18+00:00

Having a bit of trouble moving objects I’ve created into an array. So what

  • 0

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 larger 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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T00:40:18+00:00Added an answer on May 24, 2026 at 12:40 am

    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 + '}';
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of trouble loading pages into an already-existing colorbox. I have
I'm having a bit of trouble inserting into a sqlite3 database with pdo. You'll
Having a bit of trouble here. I've created a custom ImageView by subclassing ImageView
I'm having a bit of trouble with my ASP.NET Repeater control. I need to
Having a bit of trouble with the syntax where we want to call a
Having a bit of trouble using the List.Find with a custom predicate i have
Im having a bit of trouble getting my JSON to be recognised by my
I'm having a bit of trouble with the UTL_MAIL package in Oracle 10g, and
I'm having a bit of trouble trying to get class variables to work in
I'm having a bit of trouble iterating and retrieving width() values. $(.MyClass).each( function ()

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.