Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Inner class initialization and declaration

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Inner class initialization and declaration

    Hi,

    When I do some coding on an existing project, I see some code like below inside a java method,

    class_name newClass = new class_name(){
    implementation of class_name...
    };

    can anyone explain me what it is and what is purpose of doing this?

    Thanks.


  2. #2
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Inner class initialization and declaration

    It's an anonymous inner class. Sometimes you need a bare minimum of functionality that fulfils an interface and will only be instantiated once. GUI click-handlers are a perfect example. Usually every clickable control has a unique function - it would be a pain in the bum to write separate classes for each of them and offers a redundant facility: the option to instantiate more than one click-handling object.

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,256
    My Mood
    Hungover
    Thanks
    142
    Thanked 618 Times in 529 Posts

    Default Re: Inner class initialization and declaration

    Additionally, here's some recommended reading:
    JavaRanch Campfire - Getting in Touch with your Inner Class
    Anonymous inner classClass DefinitionJava Tutorial
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. What is the problem with this following declaration in JDK 1.4?
    By Senjuti in forum What's Wrong With My Code?
    Replies: 2
    Last Post: September 2nd, 2011, 11:37 AM
  2. Initialization of a class
    By Merik in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 28th, 2010, 07:18 PM
  3. [SOLVED] JUnit initialization error
    By raphytaffy in forum Java IDEs
    Replies: 2
    Last Post: September 20th, 2010, 05:33 PM
  4. Initialization parameter of servlets
    By rakesh in forum Java Servlet
    Replies: 3
    Last Post: April 13th, 2010, 03:14 AM
  5. Re-Initialization of FrameView app components??
    By DarkJoy in forum Java Networking
    Replies: 2
    Last Post: November 13th, 2009, 12:19 AM