Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to create an array inside an object/class in JAVA?

First of all, I’m kinda new in programming and my knowledge in the classes field is limited. I’ve been looking for the answer myself but I use to end up either in too basic array tutorials or more complicated array threads.

I just want to create an object called ‘Legends’ where I could create some arrays inside to insert specific phrases for each "Legend" (football players), not having to call those phrases outside the class, saving me some headaches:

public class Legends {
  String name;
  int shot;
  int accuracy;
  ...
  String shots[]; //where I want to write some specific phrases when the player shots.

  Legends(String name, int shot, ... , String shots[]) {
    this.name = name;
    this.shot = shot;
    this.shots = shots; //this is where problems start. I don't know how to "declare" it.
  }
}

Now I want to create, lets say, Messi:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Legends messi = new Legends ("Messi", 9, .... , shots[]? / {"He did it, it's goal!", "Almost!"}); //neither of that options work.

I start getting some errors I don’t even know. The first and more understandable is to make shots[] static, but I NEED specific phrases for each "Legends" I create, like messi.shots[] are going to be different than cristiano.shots[] or maradona.shots[].
How should I do it?

Please, note this is part of a very simple homework, a 1v1 against computer or another player, made into the very compilator from Visual Studio Code. It’s just I wanted to toy around creating classes to make connections and callings easier in long terms.

I’d be so grateful for your help, honestly, I’m getting hella stucked and frustrated 🙁

>Solution :

Instead of passing an array of sting while creating object of Legends like

{"He did it, it’s goal!", "Almost!"}

Pass it like

new String[]{"He did it, it’s goal!", "Almost!"}

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading