• Post Reply Bookmark Topic Watch Topic
  • New Topic

what does this code mean  RSS feed

 
Rounak Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The line in the code above
TableCell<S, T> cell = new TableCell<S, T>() {

Basically, I don't understand how curly braces follow an assignment statement?
What does the line of code mean?
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rounak, and welcome to the Ranch!

That's the Syntax of Anonymous Classes.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's start by indenting your code differently:-Now, imagine you had written it like this insteadNow, do you see that I have only changed the top lines; the remainder of the code is unchanged. Now you have a class which extends Table‍Cell. And what you had before was the same: a class which extends TableCell. Only in the first case, you don't have a name for it. This is called an anonymous class. You have overridden one method (at least in the code you showed), and all the other methods are unchanged. You can read about it in the Java® Tutorials.

And welcome to the Ranch
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
By the way, where did you find that code and how old was the source? I would have thought you would use S or T as the parameter for that method rather than Object.
 
Rounak Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you very much for the prompt reply.
I found the code here: https://docs.oracle.com/javafx/2/fxml_get_started/fxml_tutorial_intermediate.htm
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome

That is quite recent code, and I see there is a cast to T. See what happens if you change the parameter from Object to T. It might all go horribly wrong!

I tried to find an updateItem method in Table Cell, and missed it several times; it would appear to be this. Read that second link carefully.
 
Rounak Jain
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

This is what I have understood so far:
1. Anonymous class was used to override a method without creating a new class (as you mentioned earlier)
2. public void updateItem(Object item, boolean empty) {
Campbell Ritchie wrote:
See what happens if you change the parameter from Object to T.

Doing away with "Object" cleared my way to move further. Of course, there may be some reason behind using Object, but using T follows the rule: "The argument list should be exactly the same as that of the overridden method."
3. Then (item == getItem())
As per the updateItem definition (http://docs.oracle.com/javafx/2/api/javafx/scene/control/Cell.html#updateItem%28T,%20boolean%29):
item is new item for the cell

As per javadoc
public final T getItem()
// Returns the data value associated with this Cell.
So basically, it seems that if the item (content of the cell) is not changed, just exit the updateItem method. I am not clear when updateItem method gets called. Does it get called automatically? If so, when?
4. /*We call the super.updateItem(T, boolean) method.
If this is not done, the item and empty properties are not correctly set,
and you are likely to end up with graphical issues.*/
super.updateItem((T) item, empty);
In Java--the complete reference, 9th edition, I read that "super( ) must always be the first statement executed inside a subclass’ constructor." Does the above if statement prove that the book is wrong?
The later lines of code regarding instanceOf Node probably calls for a deeper reading into JavaFX, which I will do once I am clear with the above.

 
Junilu Lacar
Sheriff
Posts: 11477
180
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rounak Jain wrote:in Java--the complete reference, 9th edition, I read that "super( ) must always be the first statement executed inside a subclass’ constructor." Does the above if statement prove that the book is wrong?

No, it doesn't. The if statement you refer to is in a method, not a constructor.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rounak Jain wrote: . . . 1. Anonymous class was used to override a method without creating a new class (as you mentioned earlier) . . .
No. It is a way of creating a new class for local use only. you should only use anonymous classes if you are absolutely sure you won't need the same sort of thing twice, otherwise you are repeating yourself.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rounak Jain wrote: . . . using T follows the rule: "The argument list should be exactly the same as that of the overridden method." . . .
You will probably find that T is erased to Object, so both T and Object count as the same type.

You would have to check the Java® Tutorials; there are at least two generics section. Or Google for “Angelika Langer Java generics FAQ” and see whether Langer has anything to say about the type T being erased to Object. I would prefer T there because it maintains the parametrisation of the method and avoids that cast.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rounak Jain wrote: . . . I read that "super( ) must always be the first statement executed inside a subclass’ constructor." . . .
Please read that section in your book carefully again. It is possible for a constructor to start with
this(...);
so please see whether that is mentioned. Even if a constructor starts with this(...);, the constructor called is executed first, so you keep going until a superclass constructor is executed. The exact details are to be found in the Java® Language Specification, but that can be difficult to read.
 
Campbell Ritchie
Marshal
Posts: 56527
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A few minutes ago. I wrote: . . . the constructor called is executed first, so you keep going until a superclass constructor is executed. . . .
That isn't clear, is it?You can see the order the constructors are called in if you un‑comment the print lines. The escape sequence \u2011 is a non‑breaking hyphen. Note use of super keyword in toString method.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!