简体   繁体   English

将静态变量值传递给另一个类

[英]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. 在我的Java应用程序中,我试图创建用户组并根据用户所属的用户组为他们分配权限。 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. 我在从静态变量stgroupName检索值时遇到问题。 Method checkuserrights() at ItmMgt class not getting the current user's name. ItmMgt类的方法checkuserrights()未获取当前用户的名称。 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;
        }
  }

A static variable is by definition a gloabl object. 根据定义,静态变量是gloabl对象。 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. 您可以在代码中传递该对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM