Reputation: 342
i wonder what the difference is between these:
1-)
JFrame frame = new JFrame();
JLabel label = new JLabel("example");
frame.add(label);
2-)
JFrame frame = new JFrame();
frame.add(new Label("example"));
also, we can use the syntax like this:
1-)
new Timer(10, new ActionListener() {...}).start();
but why cannot we use it like this:
2-)
Timer timer = new Timer(10, new ActionListener() {...}).start(); // we cannot use it this way it has to be like:
//Timer timer = new Timer(10, new ActionListener() {...});
//timer.start();
Upvotes: 3
Views: 79
Reputation: 338336
The other Answers are correct.
Another way to think about it is that the call to the new JLabel
creates the JLabel object in memory immediately. Only after construction does the object get assigned to your JLabel label
variable. The label
variable is not your JLabel object. It is a reference, a name that can take us to the JLabel object. The JLabel object itself is floating around in memory someplace.
The JLabel object is like an astronaut on a spacewalk. Assigning that object to a label
variable is like putting the end of the astronaut's tether in your hand. Using that label
variable is like tugging on that tether to get to the astronaut.
Your alternate code passes a reference to the JLabel object off to the JFrame object. Again, the JLabel object already exists wholly and intact before the hand-off to the JFrame. After the hand-off, the local code has "forgotten" about the JLabel. No local reference variable means no way to get back to the JLabel object. The JLabel object still exists in memory some place, but is out of reach to our local code. In our spacewalk analogy, handing the reference off to the JFrame is like handing over the astronaut’s tether to somebody else.
If we kept a local JLabel reference variable and handed off to the JFrame, that would be like hooking up two tethers to our space-walking astronaut. You hold one tether in your hand (the local var) while passing the second tether to someone else (the JFrame object).
If all the references are set to null
or go out of scope, that is like releasing all the tethers to our astronaut. The object still lives in memory for a while but becomes a candidate for garbage collection. Actual removal from memory happens later, when the garbage collector runs and decides it is time to recycle that memory space.
Upvotes: 1
Reputation: 4549
the difference between these two are like this,
if you don't require the reference variable to object while sending that as an argument to a method you can directly generate a object while calling the method.
like this frame.add(new Label("example"));
but if you do require the object you would be passing as argument, it would be better to have it's reference variable so you can do something with it latter in code,
let's say you want to change some instance variable or get he status of Object
after the method completed it's task. In such cases you would require the reference variable to the Object
and yes answer to your other question is given by Ramanlfc
which says new Timer(10, new ActionListener() {...}).start();
this can be done as the method return type is ignored here, this task will be done as the statement issued,
but
Timer timer = new Timer(10, new ActionListener() {...}).start();
this is not possible as start()
does not return Timer
which is being assigned to the Timer
reference variable
Upvotes: 3
Reputation: 647
In the 2 first examples you're creating a JFrame and assigning it to a JFrame variable. Same with the label.
Timer timer = new Timer(10, new ActionListener() {...}).start();
Will not work because the return type of
start();
is void.
new Timer(10, new ActionListener() {...}).start();
Works because you never try to assign the void return value to a Timer.
Upvotes: 1
Reputation: 8354
Timer timer = new Timer(10, new ActionListener() {...}).start();
assignments are right to left ,so what your expression evaluates to should be assignable to the operand on the left of =
. start()
will not return Timer
in this case ,so it fails.
Upvotes: 2