简体   繁体   中英

Access the variable declared inside the private class in java

This is my code.

private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {                                         
    String username = "";
    String sql = "select * from userinfo where uname=? and pword=?";
    try{
        pst = conn.prepareStatement(sql);
        pst.setString(1, txt_username.getText());
        pst.setString(2, txt_password.getText());

        rs=pst.executeQuery();
        if(rs.next()){
            afterLogin pwcorrect = new afterLogin();
            pwcorrect.setVisible(true);
            this.setVisible(false);
            username = txt_username.getText();
        }
        else{
            JOptionPane.showMessageDialog(null, "Username and Password are Incorrect.");
        }
    }
    catch(Exception e){

        JOptionPane.showMessageDialog(null, e);
    }
}

I need to access the value of username from another class. Is there any way for this??? Please help me with code.

That's not just defined in the class, it's defined in a method in a class.

This isn't a "private" variable (or class), it's an invisible variable as far as other classes are concerned. Expose it with a public getter, or provide a mechanism the code can set it on something else.

Since this is an action handler you also need to make sure the variable will only be accessed after it's been set, or that however it's accessed can deal with it not having a value.


public class WhateverClassThisIs {

    private String username;

    public String getUsername() { return username; }

    private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String sql = "select * from userinfo where uname=? and pword=?";
        try {
            // As before, but:
            if (rs.next()) {
                username = txt_username.getText();
            }
        }
    }
}

public class SomeOtherClass {

    private WhateverClassThisIs theOtherClass;

    public void setTheOtherClass(WhateverClassThisIs theOtherClass) {
        this.theOtherClass = theOtherClass;
    }

    public void whatever() {
        String username = theOtherClass.getUsername();
    }
}

The other mechanism would rely on passing something in to WhateverClassThisIs with a username setter, roughly:

public class WhateverClassThisIs {

    private SomeOtherClass someOtherClass;

    public WhateverClassThisIs(SomeOtherClass someOtherClass) {
        this.someOtherClass = someOtherClass;
    }

    private void btnloginActionPerformed(java.awt.event.ActionEvent evt) {                                         
        String sql = "select * from userinfo where uname=? and pword=?";
        try {
            // As before, but:
            if (rs.next()) {
                someOtherClass.setUsername(txt_username.getText());
            }
        }
    }
}

public class SomeOtherClass {

    private String username;

    public void setUsername(String username) {
        this.username = username;
    }

    public void whatever() {
        // Do something with username--but either try
        // after you know it's been set, or by being
        // able to handle it being null/empty/whatever
    }
}

In addition, you could use an Observer , any of several Swing-ish mechanisms, and so on.

Yes there are ways to do it using reflection ... if the variable is a field.

But the best solution is change the other class so that it has accessible getter and/or setter methods ... after you have thought through all of the implications.

Breaking a classes encapsulation to access private state is something you should only do as a last resort.


If the variable is a local variable, then there is NO way that some other class or method can update it. And you can only access it from another class if that class is a locally declared anonymous inner class and the variable is declared as final . That doesn't sound like what you are trying to do here ....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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