user2068452
user2068452

Reputation: 7

How to access the variable decleared inside of private class from other classes in java?

I have login page with 2 text field and a login button. when login button is clicked, the username and password is retrieved from txtfield and check with username and password in database. if password and username is correct, it goes to main page. I was to get the username and show the name in main page. But all the code to check with database and retrive the data from database is inside the private class of buttonActionPerformed. Eventhough i retrive the username from database and store in variable, I can't access it from other class as it is private class. So what shall i do to access the variable which i declear inside the private class. I tried to declear variable outside the private class and update it inside the private class but still i get blank when i call from other class.

Upvotes: 0

Views: 160

Answers (4)

Govind Balaji
Govind Balaji

Reputation: 639

public class PrivateInformationRetreiving{
private int PrivateInfo;
public void setPrivateInfo(int PrivateInfo)
{
this.PrivateInfo=PrivateInfo;
}
public int getPrivateInfo(){return PrivateInfo;}
}

Explanation

First, the variable PrivateInfo is declared private, it means only the methods inside the class PrivateInformationRetreiving will know about PrivateInfo.
So, when we want to know about the private variablePrivateInfo, we must do it with public methods.
public elements are accessible not only in the class, but also outside the class.
So, setPrivateInfo() can access PrivateInfo as it is inside the class.
So, we have to use the mathods, setPrivateInfo() and getPrivateInfo() as interfaces to accessPrivateInfo.

Upvotes: 1

simpatico
simpatico

Reputation: 11087

So what shall i do to access the variable which i declear inside the private class. I tried to declear variable outside the private class and update it inside the private class but still i get blank when i call from other class.

The Reflection API is used to access private variables. If you had a class called Database and the private field in it was defPU, you would access it (and set it null) as follows:

Field defPUField = Database.class.getDeclaredField("defPU");
defPUField.setAccessible(true);
Field modsField = Field.class. .getDeclaredField("modifiers");
modsField.setAccessible(true);
modsField.setInt(defPUField, 10);
defPUField.set(null, testVal);

I'd recommend you just use dp4j to generate that Reflection code for you, behind the scenes:

@com.dp4j.TestPrivates
void aMethod(){
 Database.defPU = null;
}

Upvotes: 1

Dropout
Dropout

Reputation: 13866

After doing a successful log in just put the user data to the session and then redirect. You won't have to create another request to the database when you need the currently logged on user's name, so it's faster and more efficient.

Upvotes: 0

75inchpianist
75inchpianist

Reputation: 4102

if you are declaring it as private, you CANNOT access it outside the class context.

An option would be to expose public getters/setters to interact with your variable.

public class MyClass {
    private int x;
    public int getX(){
        return x;
    }
    public void setX(int value){
        x = value;
    }
}

Upvotes: -1

Related Questions