-2

For an example, I will use libgdx (game engine) but this is a question about how java works that i didn't find.

This is the soundControler class:

private final Sound s_0 = Gdx.audio.newSound(Gdx.files.internal("a.mp3"));;
private final Sound s_1 = Gdx.audio.newSound(Gdx.files.internal("a.wav"));
//lot of more variables

public void shoot(short sound){
    if(SOUND_ENDABLED) {

        switch (sound) {
            case 0:
                s_0.play();
                break;
            case 1:
                s_1.play();
                break;
        }
    }
}

So this is basically a library of sounds loaded in memory, which the program will be using constantly.

Another example:

Let's say we have an Animator class that will have a LOT of methods for every animation in the game.

Is this a good practice? Or will I be making a new copy of all the sounds and methods in memory every time I pass the soundControler as a parameter?

nbrooks
  • 18,126
  • 5
  • 54
  • 66
user5450074
  • 97
  • 1
  • 8
  • 1
    Possibly related: [Is Java pass-by-reference or pass-by-value](http://stackoverflow.com/questions/40480/is-java-pass-by-reference-or-pass-by-value)? – nbrooks Nov 19 '16 at 02:47

1 Answers1

1

I don't think the problem is "when you pass the sound controller as a parameter", but when you instantiate an instance. You're going to call those methods each time the instance is instantiated. Is it necessary?

If you make the variables static, then the code would only execute when the class is instantiated, i.e., once for the run of the program (really the execution of instantiation for that classloader, but for most purposes once for the run of the program). If they never change after they are first set, then they can be static without ill effect.

You didn't ask, but I'll comment on this anyway -- I do not understand why you (and MANY other people) use case statements in place of arrays. Put the sounds in an array, (or a hashmap, if you don't actually have convenient integers to index into an array); then simply index into that array with your sound variable and you're done. Your code is much shorter, and easier to understand, and runs at least as fast.

An uncompiled example:

private static Sound[] allSounds;
private static String[] allSoundFiles
  = { "a.mp3", "a.wav" /* ... * };

static
{
  allSounds = new Sound[allSoundFiles.length];
  index = 0;
  for (String s:allSoundFiles)
  {
    allSounds[index] = 
      Gdx.audio.newSound(Gdx.files.internal(s);
  }
}

public void shoot(short sound)
{
  if (SOUND_ENABLED) // or ENDABLED if you prefer
  {
    allSounds[sound].play();
  }
}

The only thing that grows with more sounds is the list of filenames. This assumes that all the sounds you load have a play() function, of course.

arcy
  • 12,845
  • 12
  • 58
  • 103
  • will just instantiate once but passing it to many other class, so if every time is passed as parameter java makes a copy of variables i am in truble – user5450074 Nov 19 '16 at 03:07
  • No, the code to initialize the variables is only run on instantiation; if you're only instantiating once, then the code is only executed once. If you wanted to test that, you could call a method that not only initializes the value but writes something to sysout, so you could see that written just once. A class that has a single instance in a program is called a 'singleton', if you're interested. – arcy Nov 19 '16 at 12:02