Using a string in different classes?

Discussion in 'Plugin Development' started by Blah1, Oct 16, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Blah1

    How do I use a defined string in a different class? I have String zip = args[0]; and I want to use it in a couple different classes. Also, is there a way that I can use "player" in another class? I need these for the same command..
     
  2. Offline

    AGuyWhoSkis

    public static String args;

    from here you can use: classname.args = "string";
     
  3. Offline

    Blah1

    AGuyWhoSkis can I do the same with a player (command sender)
    EDIT: it's not working. It says only final is permitted for this string
     
  4. Offline

    JPG2000

    Blah1 Where are you using it?

    If you make it (not inalize it), it may return null, if it isn't edited.
    Code:java
    1. public static String s = null;



    To access from same class:
    Code:java
    1. s = "New value here";


    Diffirent class:
    Code:java
    1. class_name_here.s = "New value here";
     
  5. Offline

    Blah1

    JPG2000 Still getting an error: it says only final is permitted.
    I'll post my code, maybe it will make it easier:
    (I want to take each of those cases and put the in another class because I need to put a lot of code there)
    Code:java
    1. package me.MirrorRealm.kPVP.events;
    2.  
    3. import java.util.ArrayList;
    4.  
    5. import org.bukkit.Bukkit;
    6. import org.bukkit.ChatColor;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandExecutor;
    9. import org.bukkit.command.CommandSender;
    10. import org.bukkit.entity.Player;
    11. import org.bukkit.event.Listener;
    12.  
    13. public class EventMain implements CommandExecutor, Listener{
    14. public final ArrayList<Player> spleefstarter = new ArrayList<Player>();
    15. public boolean eventstart = false;
    16. public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
    17. Player player = (Player)sender;
    18. public static String zip = null;
    19. if (cmd.getName().equalsIgnoreCase("event")) {
    20. if (!(player.hasPermission("command.owner") || player.hasPermission("command.admin"))){
    21. player.sendMessage("Unknown command. Type \"/help\" for help.");
    22. return true;
    23. }
    24. if (eventstart == true){
    25. player.sendMessage(ChatColor.RED + "There is already an event happening. Wait until this event is over.");
    26. }else{
    27. switch (zip){
    28. case "spleef":
    29. Bukkit.broadcastMessage(ChatColor.GREEN + "======================EVENT=================");
    30. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.AQUA + "started event: " + ChatColor.RED + "Spleef");
    31. Bukkit.broadcastMessage(ChatColor.AQUA + "Type " + ChatColor.RED + "/join " + ChatColor.AQUA + "to join!");
    32. Bukkit.broadcastMessage(ChatColor.GREEN + "============================================");
    33. eventstart = true;
    34. break;
    35.  
    36. case "lms":
    37. Bukkit.broadcastMessage(ChatColor.GREEN + "======================EVENT=================");
    38. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.AQUA + "started event: " + ChatColor.RED + "LMS");
    39. Bukkit.broadcastMessage(ChatColor.AQUA + "Type " + ChatColor.RED + "/join " + ChatColor.AQUA + "to join!");
    40. Bukkit.broadcastMessage(ChatColor.GREEN + "============================================");
    41. eventstart = true;
    42. break;
    43.  
    44. case "bowspleef":
    45. Bukkit.broadcastMessage(ChatColor.GREEN + "======================EVENT=================");
    46. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.AQUA + "started event: " + ChatColor.RED + "Bow Spleef");
    47. Bukkit.broadcastMessage(ChatColor.AQUA + "Type " + ChatColor.RED + "/join " + ChatColor.AQUA + "to join!");
    48. Bukkit.broadcastMessage(ChatColor.GREEN + "============================================");
    49. eventstart = true;
    50. break;
    51.  
    52. case "icebreaker":
    53. Bukkit.broadcastMessage(ChatColor.GREEN + "======================EVENT=================");
    54. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.AQUA + "started event: " + ChatColor.RED + "Ice Breaker");
    55. Bukkit.broadcastMessage(ChatColor.AQUA + "Type " + ChatColor.RED + "/join " + ChatColor.AQUA + "to join!");
    56. Bukkit.broadcastMessage(ChatColor.GREEN + "============================================");
    57. eventstart = true;
    58. break;
    59.  
    60. case "dodge":
    61. Bukkit.broadcastMessage(ChatColor.GREEN + "======================EVENT=================");
    62. Bukkit.broadcastMessage(player.getDisplayName() + ChatColor.AQUA + "started event: " + ChatColor.RED + "Dodge");
    63. Bukkit.broadcastMessage(ChatColor.AQUA + "Type " + ChatColor.RED + "/join " + ChatColor.AQUA + "to join!");
    64. Bukkit.broadcastMessage(ChatColor.GREEN + "============================================");
    65. eventstart = true;
    66. break;
    67.  
    68. default:
    69. player.sendMessage(ChatColor.RED + "/event [LMS, Bowspleef, Spleef, Icebreaker, Dodge]");
    70. break;
    71. }
    72. }
    73. }
    74. return true;
    75. }
    76. }
    77.  
     
  6. Offline

    Garris0n

    You can't access it because it's declared inside that method and can only be accessed inside that method.
     
    JPG2000 likes this.
  7. Offline

    JPG2000

    Garris0n Exactly what he said.

    Blah1 Also, not trying to be mean, but I would research more into java with multiple classes. This doesn't only apply to a string - if you don't know how to use a string from multiple classes, you probably don't generally know how to use multiple classes.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jun 4, 2016
  8. Offline

    Blah1

    JPG2000 likes this.
  9. Offline

    JPG2000

  10. Offline

    1Rogue

    And as advice to a starter, do not use static. Programming is all about instancing. If you need a string used elsewhere, pass it via constructor or method:

    Code:java
    1. public class Test {
    2.  
    3. String sample = "this is a sample";
    4.  
    5. public Test(String setter) {
    6. this.sample = setter;
    7. }
    8.  
    9. public Test(){}
    10.  
    11. public String getSample() {
    12. return this.sample;
    13. }
    14.  
    15. public void setSample(String setter) {
    16. this.sample = setter;
    17. }
    18. }


    Example of use:
    Code:java
    1. Test example = new Test();
    2. System.out.println(example.getSample()); // Prints: "this is a sample";
    3. example = new Test("This is a constructor made string");
    4. System.out.println(example.getSample()); // Prints: "This is a constructor made string"
    5. example.setSample("I like raddishes");
    6. System.out.println(example.getSample()); // Prints: "I like raddishes"


    Now you might be thinking "How do I get a string from the class that made the instance variable within the class?". That's simple as well, when you construct a class, you can pass a value of the class instance itself to the constructor of the class:
    Code:java
    1. public class TestTwo {
    2.  
    3. private final Project project;
    4.  
    5. public TestTwo(Project project) {
    6. this.project = project;
    7. }
    8.  
    9. public void printFromProject() {
    10. System.out.println(this.project.getSample());
    11. }
    12. }


    Which means you can do this...
    Code:java
    1. public class Project {
    2.  
    3. private TestTwo example;
    4.  
    5. public void onEnable() {
    6. this.example = new TestTwo(this);
    7. this.example.printFromProject();
    8. }
    9.  
    10. public String getSample() {
    11. return "I've been called from Project!";
    12. }
    13. }


    Et voila! You now can organize your code much cleaner than before.
     
    drtshock and Blah1 like this.
  11. Offline

    TheUpdater

    CLASS NAME: a
    public static string s = "hello world!"

    in other class do
    CLASS NAME: b

    a = namewhatever;

    then namewhatever.s = "hello no!";
     
  12. Offline

    drtshock

    TheUpdater Don't use static unless you know what you're doing. Use an instance of the string like 1Rogue described above.
     
  13. Offline

    TheUpdater

    well just saying that it works i never use static =/ almost never at least
     
  14. Offline

    1Rogue

    static will create a persistant object throughout the runtime of your code (bad thing #1) and public static is accessible to anyone with access to your code (bad thing #2). In reality, the only good use for static I've found in bukkit plugins is private counters for things in instanciable classes (SQL classes!) or for something similar to php constants, which would be "public static final".

    But using static for common objects is not only a horrible programming habit but a detrimental one at that.
     
    Blah1 and drtshock like this.
  15. Offline

    TheUpdater

    ok ty that helps alot wow thats why my server fucked up about 6 months ago a player made him op lol =/ so it was that static i did that explains alot
     
  16. Offline

    _Filip

    No, no it wasn't.
     
    1Rogue likes this.
Thread Status:
Not open for further replies.

Share This Page