First Solution
Mymethod class
package com.SetterAndGetter;
public class myMethod {
public model theModel;
public static String theName;
public void setTheName(String tempName) {
theName = "john";
tempName = theName;
theModel = new model();
theModel.setName(theName);
}
public void getTheName() {
System.out.println("The name is: " + theModel.getName());
}
}
Model Class
package com.SetterAndGetter;
public class model {
private int id;
private String name;
private int Age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
@Override
public String toString() {
return "model [id=" + id + ", name=" + name + ", Age=" + Age + "]";
}
}
Main Method
package com.SetterAndGetter;
public class mainApp {
private myMethod mm;
public void secondMethod_getTheName() {
System.out.println("the second method: "+myMethod.theName);
}
public static void main(String[] args) {
myMethod obj1 = new myMethod();
obj1.setTheName(null);
obj1.getTheName();
mainApp obj2 = new mainApp();
obj2.secondMethod_getTheName();
}
}
Second Solution
by using the
inheritance
my method class
package com.SetterAndGetter;
public class myMethod {
public static model Model = new model();
private String tempName;
public myMethod() {
}
public myMethod(String stempName) {
this.tempName = stempName;
stempName = "mike";
Model.setName(stempName);
}
public void dispMethod(){
System.out.println("Use Getter: "+Model.getName());
}
}
second method class
package com.SetterAndGetter;
public class mySecondMethod extends myMethod {
public void secondMethodDisplay() {
System.out.println("Use Second Getter: "+Model.getName());
}
}
model
package com.SetterAndGetter;
public class model {
private int id;
private String name;
private int Age;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
@Override
public String toString() {
return "model [id=" + id + ", name=" + name + ", Age=" + Age + "]";
}
}
main App
package com.SetterAndGetter;
public class mainApp {
public static void main(String[] args) {
myMethod obj = new myMethod("");
obj.dispMethod();
mySecondMethod obj2 = new mySecondMethod();
obj2.secondMethodDisplay();
}
}