Results 1 to 7 of 7
Thread: Anonymous classes ...
- 11-24-2008, 12:24 AM #1
Anonymous classes ...
Can someone explain this to me:
I spent yet another hundred dollars on books, this time on CSS and a day reading them and still have no way to put up a standard business invoice on the web without going to convoluted proprietary work...Java Code:TableModel dataModel = new AbstractTableModel() {...
I see this syntax sometimes, I have never had a clue how the new object 'enters' the curly braces. ( goes in scope ) could someone with a cs degree or equivalent field experience walk me through this? I have a professional organization with 550 members and have begun serious work on an applet with a JTable added to the root pane ( content pane ) ....
The user of the applet will enter five or six field per data row, I will try to get this to print looking like a standard business invoice. So far, nothing is of utility ....is not appealing to the client....XML Code:<tr><td></td></tr>
Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
- 11-24-2008, 12:32 AM #2
The new object enters the curly braces because of two reasons:
1.It is an interface
2.it is a class which implements an interface
In those cases you should declare the same methods which were declared in the interface and complete them.
I came across much with that,maybe somebody can add more something about that,i will also be glad to listen to.
In your case TableModel is an interface and AbstractTableModel is an object which behaves like an interface TableModel.Last edited by serjant; 11-24-2008 at 12:36 AM.
-
The object doesn't have to be either an interface or an abstract object though, right? Any object can be extended in this way, and in fact we often do this with JPanels to get a handle on the paintComponent method.
Any time new is used a new object is created. You're just doing short-hand for creating an object of a class with no name that derives from the declared class.Last edited by Fubarable; 11-24-2008 at 12:48 AM.
- 11-24-2008, 07:43 AM #4
Do you really mean applet?
An anonymous class is just a class that you don't bother to name. It is exactly the same as putting a named class inside the source code of your other class. Its just syntactic sugar. To have good form, the anonymous class should not be more than a half dozen lines, if its longer than that, name it.
The code works like any other assignment of a constructor:
just that the definition of what a Baz object is follows immediately.Java Code:Foo aFoo = new Baz();
- 11-24-2008, 07:56 AM #5
We often use the same concept of anonymous class while suplying the Comparator object for comparison on collections ..
DevZ;)
- 11-24-2008, 07:58 AM #6
- 11-27-2008, 06:02 PM #7
get a handle on interfaces
Every poster here has added to foothold on understanding what is going on. I chose applet to gain java's networking utility and make the table run-time dynamic, the types in the table should be fixed at design time - which is where I am at - but the number of rows is likely to vary in use, possibly in dramatic fashion. I thought that an applet would provide a great toolkit for both authentication and display, avoiding hardcoding of a forest of forms.
For example, being able to re-order by column header would be challenging to do in hardcode using forms. A handy local-scope class for that would simplifiy greatly. serant most closely grasps my original need, simple grasp of how calling new right before curly braces circumvent writing dedicated class. I guess what TableModel tm = new TableModel(){....} does is bring libs to bear power on the application of that class to the design without .... ( ? without what ... )
It appears here I could write some class, defining an interface, then do myUtilityClass mc = new myUtilityClass() { .... } which would bring several tools I have not thought of yet to the design. What my original post was intended to grasp how to use TableModel correctly as previous efforts did not go to effective control of Table display. ( Actually, that was to be a List at that time, viewed in list sorted bywhich was, eventually, to driveJava Code:public boolean equals(Object obj) // Indicates whether some other object is "equal to" this Comparator. { Customer c = (Customer) obj;// if(this.getKey() > c.getKey()) return false; if(this.getKey() < c.getKey())return false; if(this.getKey() == c.getKey())return true; }Right at the moment, I am doing a start from scratch(0) and sought to grasp how to use TableModel correctly.Java Code:// By default, a list allows more than one item to be selected // Also, the selected items need not be contiguous. JList northList = new JList(searchTerms); JList southList = new JList(searchFields); JList eastList = new JList(triggerWords); JList westList = new JList(stopWords );
Thus, ftr'swhich tells me that the semi-colon is replaced by curly braces leaves me stuck trying to dis-entangle the syntactic sugar. How can we define a class, then use that class as we drop in to the curly braces unless we wrote that class aforehand? I think I'm missing something here. I also think it is deep in compiler science and exposes a handy optimization that will fail if I do not grasp how TableModel gets a handle on the table it is supposed to model.just that the definition of what a Baz object is follows immediately.
I could, easily with the code skills I have attained, define the class right there - and do it with no name - using the work comfortably in the activation record. Easily. What I think I need to understand in depth is:much like the current implementation of invokeLater() .... in fact those curly-braces and semi-colon mashed together like scripting moves the mouse when there is no cat around drives me nuts as I do not undersand where control is going.Its just syntactic sugar. To have good form, the anonymous class should not be more than a half dozen lines, if its longer than that, name it.
I can grasp that it is making the repainting idempotent, that's obvious for me. It's the compiler science of what is going on that I am uncertian about. I can test this in a JFrame or JPanel, adding it to an applet later. The application must be resistant to some other stability issues, that is where the power of applet can add security due to it's authentication built-in's. All I need to do here is grasp how to load the Table and display it ... along with reloads and so on consequent to ActionEvent's that are fired.
Probably, rephrase original post title to "What is an interface?" and attack as though some Master's cs student has gone glass-eyed on data structures and needs to think about something else for awhile, showing an undergrad how to use an interface.Introduction to Programming Using Java.
Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor
Similar Threads
-
Get name of available classes
By escuja in forum CLDC and MIDPReplies: 0Last Post: 07-26-2008, 12:03 PM -
Cant run my classes
By Assaf A in forum EclipseReplies: 1Last Post: 04-22-2008, 02:31 PM -
Help with classes
By freswood in forum New To JavaReplies: 5Last Post: 04-21-2008, 03:28 PM -
Name of Anonymous class
By eva in forum New To JavaReplies: 1Last Post: 12-31-2007, 01:07 PM -
Anonymous class
By ravian in forum Advanced JavaReplies: 3Last Post: 12-25-2007, 10:56 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks