When to use Static

Discussion in 'Plugin Development' started by malikdbuseck, Jun 1, 2016.

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

    malikdbuseck

    Hello so currently in my plugin I am trying to avoid using static when possible, but I was also wondering how I can implement my Main Class into all my other classes non-static'ly and how I can implement my other classes into other classes not static'ly.

    Also i am having trouble with NullPointerException with my SQL preparedstatment and connection here.

    Code:
    public class MYSQL_InsertPlayerData implements Listener {
        public Main m;
        public MYSQL_InsertPlayerData(Main main) {
            this.m = main;
        }
        private  int air;
        private  int level;
        private  String rank;
        private  int money;
        private Connection con = Main.c;
        // CHECK IF PLAYER EXIST===========================
        public boolean playerExists(String uuid) {
            try{
            PreparedStatement stmt = con.prepareStatement("SELECT * FROM Atlas WHERE UUID=?");
            stmt.setString(1, uuid);
            ResultSet rs = stmt.executeQuery();
            rs.next();
            if (rs.next()) {
                return rs.getString("UUID") != null;
            }
            }catch(SQLException e){
                e.printStackTrace();
            }
            return false;
           
        }
     
  2. Offline

    Zombie_Striker

    @malikdbuseck
    This you already know. Just do the same thing as you did below to pass the Main class instance through the other class's constructors.

    For this, just create fields in your main class that will hold the instances of the other classes you want to access. After that, initialize those fields when you create new class instances. Once you have done this, you can use "main.[field]" to access the instances of the other classes.
     
  3. Offline

    malikdbuseck

    so I would do like:
    Code:
    public someclass class;
    But I also go a nullpointer when doing m.c to get access to Main(m) the connection(c)
     
  4. Offline

    Zombie_Striker

    @malikdbuseck
    Most likely, the reason you're getting the npe is because you have not set "c" equal to anything. You need to make sure that c is not null before you create this class.
     
  5. Offline

    malikdbuseck

    Wow excuse me for being so stupid I don't know how I missed these simple things! I got my code working without statics
     
  6. Offline

    Xerox262

    @malikdbuseck You only use static when you're sharing a variable between all instances of that class, updating it updates it in every instance of that class, if you were creating a shop and you had a class for CD then sub classes extending CD you could have a base price in CD, meaning if it changes you could update it and all CDs would have their base price updated. Probably a horrible explanation.
     
  7. Offline

    malikdbuseck

    No i think I totally get what you are saying!!
     
Thread Status:
Not open for further replies.

Share This Page