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.
}
}
Answer from L.Spillner on Stack OverflowThe 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.
}
}
Please follow the standard coding conventions. I changed your code with standard coding conventions. just copy paste it , it will work
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 settingPassowrd(){
Users user1 =new Users();
user1.setapassword("player");
SendMail mail1 =new SendMail(user1);
mail1.Sendsmail();
}
}
SendMail class
public class SendMail {
private Users user1;
SendMail(Users user1){
this.user1 = user1
}
public void sendMail(){
user1.getpassword(); //getting password
System.out.println(user1.getpassword()); // If print here I get null
}
}
Main class to test the code
public class SignupMain {
public static void main(String[] args) {
Signup signup1= new Signup();
signup1.settingPassowrd();
}
}
Whenever you write this
GetterAndSetter gs = new GetterAndSetter();
what you're doing is to create a new instance of GetterAndSetter. Two instances that you create won't have any connection between them.
Inside class A, you create a new instance, and set its value. Inside class C, you create a new instance, and read its value. But because you've got two different instances, the value you're reading isn't connected with the value you're setting.
This is roughly like:
- I buy an envelope, and put some money inside it.
- Later on, I want to get the money back, so I buy a new envelope, and look for the money inside it.
You have to be looking in the same envelope that you put the money in, if you want to find it!
In class A, your code creates a new instance of GetterAndSetter and sets a value to the property. In class B, however, your code creates again another new instance of GetterAndSetter , then gets the value.
The instances your code works with in classes A and B are not the same - hence you don't obtain the values set in A when trying to get it in B. The instance of GetterAndSetter created in B is not used anymore after the code in B exits.
To fix this, you need to pass a reference to the GetterAndSetter instance from class A to B. You can do this e.g. by passing it as a parameter to a method of B, or by creating a new instance of A in B and calling a method that provides an instance of GetterAndSetter.
An example of the first option (pass as parameter):
Class A{
...
GetterAndSetter createAndSet();
int a = 10;
GetterAndSetter gs = new GetterAndSetter();
gs.setValue(a);
return gs;
}
...
}
class B {
...
void getValueFromGetterAndSetter(GetterAndSetter gs) {
int c;
c = gs.getValue();
...
}
...
}
To connect the instances, we of course also need to have another piece of code (assuming instances of A and B exist already):
...
b.getValueFromGetterAndSetter(a.createAndSet());
...
This occurs because you created two independent instances of GettersAndSetters in Main and in AnotherClass. Look:
public static void main(String args[]){
GettersAndSetters sg = new GettersAndSetters(); //first instance
AnotherClass copyOfSG = new AnotherClass();
...
}
public class AnotherClass {
public void printout() {
GettersAndSetters gs1 = new GettersAndSetters(); //second instance
...
}
}
And you're setting properties in first instance, but attempting to read it from second.
To solve just pass first instance from Main to method in AnotherClass as suggested by Satya.
public class AnotherClass {
public void printout(GettersAndSetters sg) {
System.out.println("Age: " + sg.getAge());
System.out.println("Name: " + sg.getName());
...
}
}
and call it next way:
public class Main{
public static void main(String args[]){
GettersAndSetters sg = new GettersAndSetters();
AnotherClass copyOfSG = new AnotherClass();
...
copyOfSG.printout(sg);
}
}
In my main class, I set the setters. And I am looking for a way to access these values from AnotherClass
Replace
System.out.println(copyOfSG.printout());
with
copyOfSG.printout(sg);
and your printout() method
public void printout(GettersAndSetters gs1) {
//No need of creating GettersAndSetters object
System.out.println("Age: " + gs1.getAge());
System.out.println("Name: " + gs1.getName());
}
You CAN do either one. However, your professor might appreciate using the methods instead of the direct access. Here's why.
Let's say you have a class like this:
class SomeClass {
private int someValue;
private String someString;
public SomeClass(int someValue, String someString) {
this.someValue = someValue;
this.someString = someString;
}
public int someValue() {
return this.someValue;
}
public String someString() {
return this.someString;
}
public String toString() {
return someValue + ": " + someString;
}
}
It's pretty straightforward, right? Well, what if all of a sudden we want to CHANGE the implementation of how we calculate someValue, and base it off of someString:
public int someValue() {
int value = 0;
for(int i = 0; i < someString.length; i++) {
if(someString.charAt(i) == ' ') value++;
}
return value;
}
Now you also have to change every place where variable someValue was used.
So if you want to make the code easier to maintain in the long run, use the methods calls. This way when you code changes on you (and trust me, it changes all the time) you only have to change it in one spot instead of two.
And yes, you would want to use a method call in getting someString instead of the direct access in the last method :-)
When I design a class I try to make a clear distinction between the inside (implementation details) and the outside (the interface exposed to the world). Getters and setters provide a convenient place to convert values between the form in which they are stored in the objectโs instance members and the form in which the outside world sees them. Using getters and setters internally would muck that up, because they'd be getting used by both the inside and outside.
If you find yourself wanting to hide part of a class from another part of the same class, consider breaking off the part you want to hide into its own class.