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

  • SEARCH
  • Home
  • 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 7783467
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:50:52+00:00 2026-06-01T19:50:52+00:00

I’ve just been trying to learn and messing around with code. And I’ve come

  • 0

I’ve just been trying to learn and messing around with code. And I’ve come across something I did not expect to happen. I have a JLabel in a MainApp class, I create an ActionListener (HelloListener) which is passed the JLabel. When the button is pressed, the actionPerformed method should update the JLabel to “Hello again!”. And it does, but why it does it confuses me.

However, I thought I would have to return the new JLabel? When I pass the HelloListener the JLabel, isn’t that JLabel the property of the HelloListener class after it is passed? So when it updates it will only update the one in HelloListener, and I would then have to return it?

Why when I update the JLabel in the HelloListener does it also update in the MainApp class?

Here’s the code:

public class MainApp extends JFrame {

    public static void main(String[] args) {

        new MainApp();

    }

    public MainApp() {

        setLayout(new GridLayout (2,1));

        setSize(200,200);

        JLabel jl = new JLabel("Hello!");
        add(jl);

        JButton jb = new JButton("Click me!");
        jb.addActionListener(new HelloListener(jl));
        add(jb);

        setVisible(true);

    }

}

and

public class HelloListener implements ActionListener {

    JLabel jl;

    public HelloListener(JLabel jl) {
        this.jl = jl;
    }

    @Override
    public void actionPerformed(ActionEvent arg0) {
        jl.setText("Hello again!");
    }

}
  • 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-06-01T19:50:54+00:00Added an answer on June 1, 2026 at 7:50 pm

    When I pass the HelloListener the JLabel, isn’t that JLabel the
    property of the HelloListener class after it is passed? So when it
    updates it will only update the one in HelloListener, and I would then
    have to return it?

    This is where the breakdown in your understanding is. When you “pass the JLabel”, you are passing a reference to the JLabel (you are actually passing the reference by value, which you should look up as soon as you understand your current issue, because it is very important with Java). The underlying object instance still exists everywhere it did before. So the JLabel jl in the HelloListener instance is just a reference to the same actual JLabel instance that is presented in the gui.

    This is fundamental to how Java (and many programming languages) works. If you do

    Dog d = new Dog();

    and then pass d to a method

    walk(d);

    and walk looks like

    public void walk(Dog dog) {
       dog.setLastWalkTime(now());
       dog.attachLeash(); 
    }
    

    dog in the method, and d in the calling scope, both point to the same Dog instance. So as sson as you set the lastWalkTime, the underlying instance is modified. If right after walk you looked at d, you would see the lastWalkTime value you set in the method.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I am trying to render a haml file in a javascript response like so:
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString

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.