1

Simply put here is what I am trying to do:

class Main { 
    public static void main (String[] args) {
        List option = new List();
        SetGet call = new SetGet();
        option.names();
        call.getName();
    }
}

class List {
    public void names() {
        SetGet tell = new SetGet();
        tell.setName("Frank");
    }
}

class SetGet() {
    private String info;

    String getName() {
        return info;
    }

    public void setName(String name) {
        info = name;
    }
} 

Essentially I have a main method that will tell another method from another class to set the name of something. Yet whenever I request getName from the main method, it always comes back null even though I already set a name for it. This is a really simplified version of what I am trying to do. Any assistance is greatly appreciated.

2
  • You are creating two separate instances of SetGet (using new SetGet()). And the names() method creates an instance, sets the name, but then discards the newly created instance, because you don't return it or store it in a field. Commented Jan 9, 2022 at 15:03
  • Thanks for the quick reply. I am new to programming, how would I call the value of a variable set by another method from a different class? Commented Jan 9, 2022 at 15:07

2 Answers 2

1

When you call option.names(); in your main method, it creates a new SetGet object named tell and sets its name to "Frank".

After that you call call.getName(); on a newly created SetGet named call.

Both the SetGet objects are not related, they are different.

Sign up to request clarification or add additional context in comments.

Comments

0

You creating 2 different objects of SetGet. You can try to return the object tell and overwrite call with the returned object.

class Main {
  public static void main (String[] args) {
    List option = new List();
    SetGet call =  new SetGet();
    call = option.names();
    System.out.println(call.getName());
  }
}

class List {
  public SetGet names() {
    SetGet tell = new SetGet();
    tell.setName("Frank");
    return tell;
  }
}

class SetGet {
  private String info;

  String getName() {
    return info;
  }

  public void setName(String name) {
    info = name;
  }
}

2 Comments

But what if under the class List, I tell the method SetGet() to set other variables like tell.setAge and tell.setYearBorn, would the return tell be able to return all those values for me? If you can point to me what to research, that would be greatly appreciated. Just trying to learn as much as I can.
First of all SetGet is a class not a method. The object tell in list is a local object just for the method names. if you want more methods overwriting the same object you should create tell as a local object of List, then if you add more methods to List the returned tell would always have the same properties for the object of List(in this case call). For better understanding try to search for object oriented programming.

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.