Reputation: 121
public class Sample1 {
public String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public static void main(String[] args) {
Sample1 s1= new Sample1();
s1.setName("Abc");
}
}
public class Sample2 {
public static void main(String[] args) {
Sample1 n2= new Sample1();
System.out.println(n2.getName());
}
}
I have two classes Sample1 and Sample2 two. I am allocating string value using setter method and returning in another class by using getter method, but an output is null. Why null is an output and how to get string value from one call to another class?
Upvotes: 0
Views: 5198
Reputation: 2579
Consider the code :
Sample1 n2= new Sample1();
System.out.println(n2.getName());
Here the Name is not set , So you need to set the name before getting the name.
Sample1 n2= new Sample1();
n2.setName("name goes here");
System.out.println(n2.getName());
Also, you can try parameterized constructor in the Sample1 class and access like in sample2 class:
Sample1 n2= new Sample1("your name goes here");
System.out.println(n2.getName());
The constructor will be :
public Sample2(String name){
this.name = n;
}
3 thing you can add method in Sample1 class and access it in Sample2 class.
I don't want to set String value in Sample2 class, need to assign string value in Sample1 only, after that i need that string value in Sample2 class
public class Sample1 {
private String _name;
public String getName() {
return _name;
}
private setName(String name) {
_name = name;
}
public SetNameHelper(){
setName("somestring");//You will be setting the name in Sample 1
}
}
public class Sample2 {
public static void main(String[] args) {
Sample1 n2= new Sample1();
n2.SetNameHelper();
System.out.println(n2.getName());//You will be getting Name in Sample 2 class
}
}
Upvotes: 1
Reputation: 690
I think I understand you confusion: you mistake a main function for a constructor!
I noticed that you created a main
function in each class, whose only role is to create an instance and set the internal fields of the class. It's probably a Constructor you were looking for.
Here's the deal:
main
method per project.So here is your example, fixed (I believe), in a way that can bring sample1 value into an sample2, as you say:
public class Sample1 {
public String name;
public String Sample1(String initialName){
this.name = initialName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class Sample2{
public String name;
public String Sample2(String initialName){
this.name = initialName;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
public class ProgramEntryPoint{
public static void main(String[] args) {
Sample1 s1 = new Sample1("a name");
System.out.println("Initial sample1 name: " + s1.getName());
s1.setName("a New Name!");
System.out.println("New sample1 name: " + s1.getName());
Sample2 s2 = new Sample2(s1.getName());
System.out.println("Initial sample2 name: " + s2.getName());
}
}
Which, once you run ProgramEntryPoint.main, will print:
Initial sample1 name: a name
New sample1 name: a New Name!
Initial sample2 name: a New Name!
This is all simple Java stuff, you should read up a few basic tutorials (the ones on oracle website are a good start)
Upvotes: 0
Reputation: 200
class Sample2 {
Sample1 sample1;
Sample2(Sample1 sample){
this.sample1 = sample;
}
private String getSample1Name() {
return this.sample1.getName();
}
public static void main(String[] args) {
Sample1 sample1 = new Sample1();
sample1.setName("Abc");
Sample2 n2= new Sample2(sample1);
System.out.println(n2.getSample1Name());
}
}
Upvotes: 0
Reputation: 161
I think you misunderstood the main method, maybe I am wrong, however only one main method is executed.
If you run Sample2.main - on Sample1 you are not setting a name so it is null (Sample1.main is never executed).
If you run Sample1.main - Sample1 is created and assigned a name.
So either assign the name in the Sample2.main:
public static void main(String[] args) {
Sample1 n2= new Sample1();
n2.setName("xxx");
System.out.println(n2.getName());
}
or do it via constuctor.
public class Sample1 {
private final String name;
public String getName() {
return name;
}
public Sample1(String name) {
this.name = name;
}
}
Upvotes: 1