Results 1 to 7 of 7
  1. #1
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Question Anonymous classes ...

    Can someone explain this to me:
    Java Code:
    TableModel dataModel = new AbstractTableModel() {...
    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...

    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 ....
    XML Code:
    <tr><td></td></tr>
    is not appealing to the client....
    Introduction to Programming Using Java.
    Cybercartography: A new theoretical construct proposed by D.R. Fraser Taylor

  2. #2
    serjant's Avatar
    serjant is offline Senior Member
    Join Date
    Jun 2008
    Location
    Ukraine,Zaporozhye
    Posts
    487
    Rep Power
    7

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    I see this syntax sometimes, I have never had a clue how the new object 'enters' the curly braces.
    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 01:36 AM.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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 01:48 AM.

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by Nicholas Jordan View Post
    Can someone explain this to me

    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.
    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:

    Java Code:
    Foo aFoo = new Baz();
    just that the definition of what a Baz object is follows immediately.

  5. #5
    DevzAbhi's Avatar
    DevzAbhi is offline Member
    Join Date
    Jul 2008
    Posts
    67
    Rep Power
    0

    Default

    We often use the same concept of anonymous class while suplying the Comparator object for comparison on collections ..
    DevZ;)

  6. #6
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by DevzAbhi View Post
    We often use the same concept of anonymous class while suplying the Comparator object for comparison on collections ..
    Yes, that is a great example, a good Comparator class should be tiny, perhaps just doing a compareTo() with different ordering. Too small to be worth naming.

  7. #7
    Nicholas Jordan's Avatar
    Nicholas Jordan is offline Senior Member
    Join Date
    Jun 2008
    Location
    Southwest
    Posts
    1,018
    Rep Power
    8

    Thumbs up 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 by
    Java 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;
         }
    which was, eventually, to drive
    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    );
    Right at the moment, I am doing a start from scratch(0) and sought to grasp how to use TableModel correctly.

    Thus, ftr's
    just that the definition of what a Baz object is follows immediately.
    which 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.

    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:
    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.
    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.

    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

  1. Get name of available classes
    By escuja in forum CLDC and MIDP
    Replies: 0
    Last Post: 07-26-2008, 01:03 PM
  2. Cant run my classes
    By Assaf A in forum Eclipse
    Replies: 1
    Last Post: 04-22-2008, 03:31 PM
  3. Help with classes
    By freswood in forum New To Java
    Replies: 5
    Last Post: 04-21-2008, 04:28 PM
  4. Name of Anonymous class
    By eva in forum New To Java
    Replies: 1
    Last Post: 12-31-2007, 02:07 PM
  5. Anonymous class
    By ravian in forum Advanced Java
    Replies: 3
    Last Post: 12-25-2007, 11:56 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •