1

I don;t understand how we can use same JLabel variable to create a new label. Doesn't aLabel variable here refers to the same single object(label). So how could we get 4 label ?

Can i do this behavior with other classes objects to or just with component classes?

Explanation of Step by step flow of execution of code here would be helpful.

 JLabel label1 =newJLabel("First label"); 
add(label1, BorderLayout.NORTH); 
JLabel label2 =newJLabel("Second label"); 
add(label2, BorderLayout.SOUTH); 
JLabel label3 =newJLabel("Third label"); 
add(label3, BorderLayout.CENTER); 

OR i can just use:

JLabel aLabel =newJLabel("First label"); 
        add(aLabel, BorderLayout.NORTH); 
        aLabel =newJLabel("Second label"); 
        add(aLabel, BorderLayout.SOUTH); 
        aLabel =newJLabel("Third label"); 
        add(aLabel, BorderLayout.CENTER);
mhrzn
  • 379
  • 1
  • 4
  • 18
  • In this scenario, it's the `new` keyword which assigns some memory location for the object and passes this reference to the left had side variable. In this you simply using this reference to tell the layout where to put this object at, then you change the contents of this variable by using again `new` will puts another value and similarly the whole procedure is repeated again. Second approach, gives more clarity to the ____reader/maintainer___ of the code, IMHO – nIcE cOw Jul 07 '14 at 16:50
  • @nIcEcOW - should the 1st object referencing by aLabel be destoryed as aLabel is not referencing to it any more but referencing it to 2nd label object – mhrzn Jul 07 '14 at 17:00
  • Naa, since now `LayoutManager` holds the reference to the same, so atleast somewhere someone is holding that reference. SO it won't be destroyed. – nIcE cOw Jul 07 '14 at 17:01
  • This is very similar to one [query](http://stackoverflow.com/q/10051638/1057230), that I tried to respond to, for someone. Might be it will be helpful for this scenario too, I HOPE :-) – nIcE cOw Jul 07 '14 at 17:20

3 Answers3

1

In this case, aLabel is just a reference to an object in memory. When you do:

aLabel = newJLabel("Second label");

You are changing the reference to point to a totally different object that the first label. Essentially what this says is "Take the thing on the right hand side of the equation and make aLabel point to it." Then, when you call

add(aLabel, BorderLayout.SOUTH);

You are actually adding an entirely different object than during the first call.

In the other case you simply make a new reference for every object.

  • should the 1st object referencing by aLabel be destoryed as aLabel is not referencing to it any more but referencing it to 2nd label object – – mhrzn Jul 07 '14 at 17:13
  • @mhrzn No, because the JPanel is now holding a reference to it since add(aLabel, BorderLayout.SOUTH) is called. Had we not made that call, then the object would have been automatically destroyed the garbage collector. –  Jul 07 '14 at 17:43
1

You need to remember that the UI holds a reference to the label object when it's added - it doesn't just get "forgotten" for you to reuse afterwards. In your second case, you're just changing the name of the same label and adding it to multiple places in the UI - but when you're changing the label's text, you're altering the exactly the same object that you've already added in other places in the UI.

You could theoretically safely reuse JLabels, but only when nothing else (including the UI) holds a reference to it. In practice, don't bother - it creates messy, bug prone code. If you want a new label somewhere, just create a new label object and let the GC clear any unused, old ones.

Michael Berry
  • 70,193
  • 21
  • 157
  • 216
1

Better yet, why not?

    add(new JLabel("First label"), BorderLayout.NORTH); 
    add(new JLabel("Second label"), BorderLayout.SOUTH); 
    add(new JLabel("Third label"), BorderLayout.CENTER);

of course, if you ever needed a reference to that JLabel, then you'll be out of luck (because you failed to hold a reference in the Object).

That's the real motivator for the many JLabel variables, they want to hold references to each created JLabel. Perhaps those references are not being used for any specific purpose, but many people create them "just in case" they need a handle for one of the JLabels at a future date.

At some point you need to cut your losses and go with it. Perhaps it is important to optimize in this manner, perhaps it is not. If it is important, then you typically write some code to prove the improved performance. If you didn't write that code, then you can't prove you made anything better.

The funny thing is that in writing that code to track the kind of performance you care about, you quickly discover that many areas of the code were some performance impacting issue exists are irrelevant; because they are so seldom used, leading to seldom feeling the impacts, or they are greatly overshadowed by much bigger issues.

Changing without doing some sort of analysis sometimes can provide solutions, but often it is just a shot-in-the-dark approach, akin to premature optimization.

Edwin Buck
  • 69,361
  • 7
  • 100
  • 138