Link to home
Start Free TrialLog in
Avatar of Axter
AxterFlag for United States of America

asked on

Inline declarations

I was studying and trying to break down the code below, when I realized the code had some strange inline declarations.
The new Object and new String are simple enough to figure out, but what I don't understand is the section of code that comes after that.

1.  Why is there a set of {} for this section of code?
2.  What is [public Class getColumnClass] doing in the code?
3.  What is [Class[] types] doing in the code?
4.  Is this section of code declaring some type of inline class, and if so, can someone give me a link for a good tutorial on this?

        jTable1.setModel(
                            new javax.swing.table.DefaultTableModel
                            (
                                new Object [][] {
                                    {"11:00", "C++", new Integer(50), "1111111111111111111111111111"},
                                    {"1111", "1111", new Integer(111), "11111"},
                                    {"222", "22", new Integer(222), "22222222222222222222"},
                                    {"333", "333", new Integer(33), "3333333333333333333333"},
                                    {"44", "44", new Integer(4), "4"},
                                    {"5", "5", new Integer(5), "5"},
                                    {"6", "6", new Integer(6), "6"}
                                },
                                new String [] {"Time", "Topic Area", "Points", "Title"}
                            )
                            { //What is this *****************************************************
                                Class[] types = new Class [] {java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.String.class};
                                public Class getColumnClass(int columnIndex) {return types [columnIndex];}
                            }//**************************************************************
                        );
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

The code is creating a DefaultTableModel using this ctor

http://java.sun.com/j2se/1.5.0/docs/api/javax/swing/table/DefaultTableModel.html#DefaultTableModel(java.lang.Object[][], java.lang.Object[])

and overriding the methods it declares
More specifically

>>1.  Why is there a set of {} for this section of code?
Because arrays are being declared as parameters to DefaultTableModel ctor

>>2.  What is [public Class getColumnClass] doing in the code?
It's allowing the view components (connected with a JTable) to discover the class of data held for each column so it can render it

3.  What is [Class[] types] doing in the code?
>> It's declaring an array so it can return the appropriate type for the column (see 2. above)
4.  Is this section of code declaring some type of inline class, and if so, can someone give me a link for a good tutorial on this?
See my previous posting and here's a link
http://java.sun.com/docs/books/tutorial/uiswing/components/table.html

Avatar of Axter

ASKER

>>and overriding the methods it declares

That's the part I don't understand
Avatar of Axter

ASKER

So is this an inline subclass?
And if so, what is the supperclas?
ASKER CERTIFIED SOLUTION
Avatar of CEHJ
CEHJ
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
:-)