Home > Enterprise >  How to access variable from method in JDA
How to access variable from method in JDA

Time:05-30

I am making a Discord bot, and I'm stuck. I have 2 methods that are in the same file.

Method 1:

@Override
public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
    int a = Integer.parseInt(event.getValues().get(0));
}

Method 2:

@Override
public void playlistLoaded(AudioPlaylist playlist) {
    final AudioTrack Track = playlist.getTracks().get(a);
}

Question: How can I access variable a from method 1?

CodePudding user response:

Try to set it as a field

public class SomeClass extends ListenerAdapter {
     private int track;
     @Override
     public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
          track = Integer.parseInt(event.getValues().get(0));
     }
     @Override
     public void playlistLoaded(AudioPlaylist playlist) {
          final AudioTrack Track = playlist.getTracks().get(track);
     }

Or using a stack

public class SomeClass extends ListenerAdapter {
     private final Stack<Integer> tracks = new Stack<Integer>();
     @Override
     public void onSelectMenuInteraction(SelectMenuInteractionEvent event) {
          int track = Integer.parseInt(event.getValues().get(0));
          tracks.push(track);
     }
     @Override
     public void playlistLoaded(AudioPlaylist playlist) {
          final AudioTrack Track = playlist.getTracks().get(tracks.pop());
     }
  • Related