amal
amal

Reputation: 3582

Passing static variable value to another class

In my Java Application Im trying to create user groups and assign permission to users depending on the user group they belongs to. This is how I programmed. When User Login to the system, grab the user name and store in a static variable. When user opens any Form, get the user name from static variable and check its group and permissions. Depending on the permissions disable and enable some components of the form. Here Im having problem retrieving the value from static variable stgroupName. Method checkuserrights() at ItmMgt class not getting the current user's name. Can someone please assist me to understand whats the wrong here. I guess, there should be many better ways to do this. So any suggestions welcome.

  public class Login extends javax.swing.JInternalFrame {

        public static String USERNAME;
        USERNAME  = txtUserName.getText(); // get the user name to static variable
  }

  public class ItemMgt extends javax.swing.JInternalFrame {

        public ItemMgt() {

              initComponents();
              checkuserrights();
              genarateID();
        }

  private void checkuserrights() {
        try {

              Usergroup usergroup = UserGroupController.getUserRights(Login.USERNAME);// check the user's rights passing user name from static variable.
              if (usergroup != null) {
                    btnDelete.setEnabled(usergroup.isDeleteItem());
                    btnAdd.setEnabled(usergroup.isAdditem());
                    btnUpdate.setEnabled(usergroup.isUpdateitem());

              }
        } catch (ClassNotFoundException ex) {
              Logger.getLogger(ItemMgt.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
              Logger.getLogger(ItemMgt.class.getName()).log(Level.SEVERE, null, ex);
        }

  }

  public class UserGroupController {

        public static Usergroup getUserRights(String username) throws ClassNotFoundException, SQLException {
              Connection conn = DBConnection.conn();
              String sql = "select * from UserGroup where uGroupName = ?";

              Object[] values = {username};
              ResultSet res = DBHandller.getData(sql, conn, values);
              while (res.next()) {
                    String grpName = res.getString("uGroupName");
                    boolean additem = res.getBoolean("addItem");
                    boolean delitem = res.getBoolean("delteItem");
                    boolean upitem = res.getBoolean("editItem");
                    Usergroup ugroup = new Usergroup(grpName, additem, delitem, upitem);
                    return ugroup;
              }
              return null;
        }
  }

Upvotes: 0

Views: 5541

Answers (1)

Devolus
Devolus

Reputation: 22084

A static variable is by definition a gloabl object. When you change the value in one instance, then the value of all other instances of that object will have the same value. That's the point of using a static.

It sounds to me that your object design is wrong and you should use an object having information about a user which would include the groupname. This object you can pass around in your code.

Upvotes: 3

Related Questions