Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6864315
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:53:46+00:00 2026-05-27T02:53:46+00:00

How can I update an object in class A from a method in class

  • 0

How can I update an object in class A from a method in class B without using the return?

for example:

public class A {
//my main class

private javax.swing.JTextField txtField1;
//a text field (txtField1) is initialized in this class and drawn

}


public class B {

public void doSomething(){

    //does something and updates the txtField1 in class A

}

}

and once again, I do not wish to use return since my return is already returning another value from the same method.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T02:53:46+00:00Added an answer on May 27, 2026 at 2:53 am

    There are many ways you could achieve this. The simplest would be to pass the object into the method in class B:

    public void doSomething(JTextField fieldToUpdate){
       //your logic here
       fieldToUpdate.setText("something");
    }
    

    Then you can just update fieldToUpdate directly. This is not a great design pattern since it directly exposes control of a variable owned by 1 class to another.

    Another alternative is to pass the instance of Class A into the method and call public methods on it:

    public void doSomething(A aInstance){
        //your logic here
        aInstance.setText("something");
    }
    

    then in class A you’d need to define

    public void setText(String text){
       txtField1.setText(text);
    }
    

    This is a little better since class B doesn’t have direct access to the internals of Class A.

    An even more encapsulated response (though probably overkill for a case this simple) is to define an Interface and pass an instance of a class that implements the interface to the method in class B:

    public void doSomething(TextDisplayer txt){
      //your logic here
       txt.setText("something");
    }
    

    then in class a:

    public class A implements TextDisplayer{
    
     public void setText(String txt){
      txtField1.setText(txt);
    }
    
    }
    

    then the interface:

    public interface TextDisplayer{
     public void setText(String txt);
    }
    

    The advantage of this approach is that it keeps class B completely decoupled from the class A. All it cares about is that it is passed something that knows how to handle the setText method. Again, in this case it is probably overkill, but it is the approach that keeps your classes as decoupled as possible.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I use automapper to update the properties values of another object without
In LINQ to Object context ,can i update an object in memory. I mean
I'm trying to integrate a NSURLConnection object with UIProgressView, so I can update the
I'm generating ICalendar (.ics) files. Using the UID and SEQUENCE fields I can update
I need to call properties and functions of an object from a different class.
I can't currently understand why my update method in my repository for the style
I am getting list of data from database from my Service Class Method mDataVO.getMarketData()
EntityManager.merge() can insert new objects and update existing ones. Why would one want to
How can i make a cached object re-cache it self with updated info when
How can update a input hidden inside an UpdatePanel on an AsyncPostBack? The user

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.