0
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?

6
  • Similar :stackoverflow.com/questions/39659795/… Commented Sep 23, 2016 at 12:34
  • 2
    You have two main functions? Commented Sep 23, 2016 at 12:35
  • Please format your code when posting. It's really hard to read without indentation. Commented Sep 23, 2016 at 12:36
  • Are you running the code twice, once running Sample1 and the other time running Sample2, but expecting everything to stay set between runs? Commented Sep 23, 2016 at 12:37
  • 1
    @Venkat remarkably so. Commented Sep 23, 2016 at 12:38

4 Answers 4

1

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;
  }
}
Sign up to request clarification or add additional context in comments.

6 Comments

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
Then you need to store that value somewhere between the runs.
you have to do it in the constructor of the Sample1 then. public Sample1() { this.name = "xxx";}
I have the only possibility to add string value in Sample1 class only but need that value in Sample2 class
@Smileysmile check this answer 3 rd part stackoverflow.com/a/39661060/5395773
|
1

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
    }
}

2 Comments

It is showing and error at setName("somestring"); line
@Smileysmile can you please share the error details
0
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());
}

}

3 Comments

Thank you, but my requirement is I have the only possibility to set the string value in Sample1 class only need that value in Sample2 class.
Sample1 and Sample2 are two different JAVA programs you cannot share data between them as such unless you go for advanced concepts like making them as Threads or Socket programming..
My requirement needed sample1 class String value into Sample2 class, How it is possible?
0

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:

  • A main method is the entry point of a program. It just stays, to run me: begin executing this code. You probably (99.9% of the case) need only one main method per project.
  • A Constructor is a method which creates an instance of, each class, i.e. an object you can manipulate elsewhere in your code. Read up on Object-Oriented Programming

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)

1 Comment

My requirement needed sample1 class String value into Sample2 class, How it is possible?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.