0

I'm pretty new to Java (and first question I ask on Stackoverflow, too) and right now I'm struggling about how to pass some variables values defined in a method to another.

I've done multiple searchs about things like global variables, ArrayList, HashMap, etc, but the only thing that seems to be what I'm searching ( Global variables in Java) let me a little more confused about how to proceed.

I've try to use ArrayList for that, but it haven't work - and I don't know if I can use it for what I want to day anyways...

Here's my code:

public static void creationGuilde(String[] args, Player player, String playerName)
{
    String nomGuilde = args[2];
    String message1 = "Votre nouvelle guilde se nommera " + nomGuilde + ".";
    TextComponent confirmer = new TextComponent("Cliquez ici pour confirmer");
    TextComponent annuler = new TextComponent("cliquez ici pour annuler");
    String message2 = confirmer + "OU" + annuler + ".";
    player.sendMessage(message1);
    player.sendMessage(message2);
    confirmer.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeConfirmer"));
    annuler.setClickEvent(new ClickEvent(ClickEvent.Action.RUN_COMMAND, "/creationGuildeAnnuler"));
}

private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event)
{
    if (event.getMessage().equals("creationGuildeConfirmer"))
    {
        String guilde = CommandeGuilde.creationGuilde(nomGuilde);
        event.getPlayer().sendMessage("Félicitations! Vous venez de créer la guilde " +guilde); // <-- Here, trying to get the value of 'guilde' in the 'creationGuilde' method...
    }
}

What I want to do is from the "onPreCreationGuildeCommand" method, I want to get the 'nomGuilde' value from the "creationGuilde" method to put it on my last sendMessage.

I hope that my question is clear enough. Thanks for helping me on this.

Community
  • 1
  • 1
Vellric
  • 19
  • 3
  • What you have looks like it should work, please edit the question and state specifically what output you get from this vs. what you expect. Also add a `print` statement to check that the value of `guilde` has something in it. – nhouser9 Mar 28 '17 at 16:28
  • I can't build my project to try it - my IDE is giving an error on this `String guilde = CommandeGuilde.creationGuilde(nomGuilde);`. saying "Cannot resolve symbol 'nomGuilde'" – Vellric Mar 28 '17 at 16:40

2 Answers2

0

You can define the variable nonGuilde outside the methods ,global variable.When you define a global variable you can "write" or take this varibale from all methodes of this class. A second solution is to return nonGuilde from creationGuilde method.

Thelouras
  • 852
  • 1
  • 10
  • 30
  • If I understand correctly, if I define the variable `nomGuilde` in the parent class of my two methods, and I write it in my `creationGuilde`one, I would be able to get it from another method? – Vellric Mar 28 '17 at 16:37
  • yes ,but maybe this variable must be static .Dont want to create object for use the variable.Just define it as static in the main body of class and you can use it in all the methods :) – Thelouras Mar 28 '17 at 16:45
0

The easiest and probably best solution is to define nonGuilde as global variable.
Code should look like this:

class YourClassName{
    //Must be null
    //otherwise if you call onPreCreationGuildeCommand before creationGuilde
    //you would get error because variable hasn't been initialized
    private String nomGuilde = null;

    public static void creationGuilde(String[] args, Player player, String playerName){
        //set the value
        //value will persist outside the method because variable is global
        nomGuilde = args[2];
    }

    private void onPreCreationGuildeCommand(PlayerCommandPreprocessEvent event){
        // Here you can do anything with variable, for example:
        System.out.println(nomGuilde);
    }
}

Also my advice to you is to read a bit about variables and their scope(block of code where specific variable is visible) and their lifetime(how long does variable exist in memory)

nhouser9
  • 6,730
  • 3
  • 21
  • 42
FilipRistic
  • 2,661
  • 4
  • 22
  • 31