Reputation: 389
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);
Upvotes: 1
Views: 420
Reputation: 70929
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.
Upvotes: 1
Reputation: 72344
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 JLabel
s, 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.
Upvotes: 1
Reputation:
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.
Upvotes: 1