Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is a very simple example of my project which is a lot more bigger in scale.

Tasks:

  • The password will be set at the setpassword method.

  • The getpassword method will return the password.

  • The password is called at sendmail class in order to be send via email to the user to log in with the new credentials.

But When I run the whole code everything works except that the sendmail class won't access the password from the getpassword method in users class.

I put a simple version of my code:

users class >>>>>

public class  users {


private String password;


public users (){}

// GETTING PASSWORD
public String getpassword(){

    return password;
}


// SETTING PASSWORD
 public void setapassword(String password){
     this.password=password;
 }




}

Signup class>>>>>>

public class signup {

public void signsup(){

    users  user1 =new users();
    user1.setapassword("player"); 

    sendmail mail1 =new sendmail();
    mail1.Sendsmail();

}
}

sendmail class>>>>

public class sendmail   {

public void  Sendsmail(){

    users user1 =new users(); // object

    user1.getpassword(); //getting password 

 System.out.println(user1.getpassword()); // If print here I get null
 }
 }

Main signup Class>>>>

public class SignupMain {

public static void main(String[] args) {

     signup signup1= new signup();
     signup1.signsup();
   }

 }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
243 views
Welcome To Ask or Share your Answers For Others

1 Answer

The user1 object from your signup class and your sendmail class are different. Surely their variable is named the same way but they refer to different Objects. To access the password from the user you have to pass the user to the sendmail class e.g.:

Signup.java

public class signup
{

  public void settingPassowrd()
  {
    users user1 = new users();
    user1.setapassword( "player" );

    sendmail mail1 = new sendmail();
    mail1.Sendsmail(user1);

  }
}

with:

public class sendmail
{

  public void Sendsmail(user usr)
  {

    usr.getpassword(); // getting password

    System.out.println( usr.getpassword() ); // will be the proper value from now on.
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...