Results 1 to 3 of 3
  1. #1
    castiel is offline Member
    Join Date
    Aug 2009
    Posts
    26
    Rep Power
    0

    Default What type of java syntax is this?

    Hey, I was reading through some java code in this program
    and I saw this strange parameter inside the parse() method invocation.
    Could you tell me what this thing is as it's the first time i've seen a variable and then a method inside the variable (referring to the proteced void onRow()). Sorry I'm pretty newbie at java and don't know how to say this properly. I wanted to google it but had no idea what to even put for the search.


    Java Code:
    this.parse(reader, new AbstractCsvEventHandler() {
    
    			protected void onRow(List row) {
    				rows.add(row);
    			}
    		});

    Cheers,
    Newbie

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    This is anonymously class.
    Skype: petrarsentev
    http://TrackStudio.com

  3. #3
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    953
    Rep Power
    5

    Default

    Actually, it's called an anonymous inner class.

    Inner Class Example (The Java™ Tutorials > Learning the Java Language > Classes and Objects)

    The parse() method you're calling requires two parameters, the second of which apparently needs to be an object of type AbstractCsvEventHandler. Now AbstractCsvEventHandler is apparently an abstract class, (or possibly a poorly named interface), so what you really need here is an object of a class that extends AbstractCsvEventHandler and implements the onRow() method. You could do that like this:
    Java Code:
    ...
        private AbstractCsvEventHandler myHandler = new MyCsvEventHandler();
    ...
            this.parse(reader, myHandler);
    ...
        private class MyCsvEventHandler extends AbstractCsvEventHandler {
            protected void onRow(List row) {
                rows.add(row);
            }
        }
    Or you could do it like this (eliminating a superfluous variable):

    Java Code:
    ...
            this.parse(reader, new MyCsvEventHandler());
    ...
        private class MyCsvEventHandler extends AbstractCsvEventHandler {
            protected void onRow(List row) {
                rows.add(row);
            }
        }
    But Java lets you take a further shortcut -- you can instantiate your new class without ever declaring it by name, as you see in the code you presented. This is called an anonymous (no name) inner (declared within a method body) class. You're telling the compiler "give me a new instance of a class with no name that extends AbstractCsvEventHandler (or implements it, if it's an interface), and that I will define right now." The braces {} surround the class definition -- which in this case is just the one method definition -- and after the closing brace, you have the closing parenthesis for the parse() method, followed, of course, by a semicolon.

    Hope that helps.

    -Gary-

Similar Threads

  1. Java Syntax If statement
    By Blasz in forum New To Java
    Replies: 9
    Last Post: 09-01-2010, 12:21 PM
  2. Specific syntax? java.util.regex.Pattern$Node
    By lmhelp2 in forum New To Java
    Replies: 1
    Last Post: 04-09-2010, 11:32 AM
  3. Java syntax I don't understand - assignment
    By poet in forum New To Java
    Replies: 2
    Last Post: 09-19-2009, 06:47 AM
  4. Java JDBC/MySQL appropriate Syntax
    By thelinuxguy in forum Advanced Java
    Replies: 7
    Last Post: 02-10-2009, 07:57 PM
  5. Replies: 4
    Last Post: 06-25-2008, 08:09 PM

Posting Permissions

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