The Artima Developer Community
Sponsored Link

Java Buzz Forum
Constructors in Anonymous Inner Classes

0 replies on 1 page.

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 0 replies on 1 page
Brian McCallister

Posts: 1282
Nickname: frums
Registered: Sep, 2003

Brian McCallister is JustaProgrammer who thinks too much.
Constructors in Anonymous Inner Classes Posted: Jan 21, 2004 2:00 PM
Reply to this message Reply

This post originated from an RSS feed registered with Java Buzz by Brian McCallister.
Original Post: Constructors in Anonymous Inner Classes
Feed Title: Waste of Time
Feed URL: http://kasparov.skife.org/blog/index.rss
Feed Description: A simple waste of time and weblog experiment
Latest Java Buzz Posts
Latest Java Buzz Posts by Brian McCallister
Latest Posts From Waste of Time

Advertisement

A coworker did something that got a good double take today... he put a "constructor" in an anonymous inner class. Java has a nice feature where you can plop down a block anywhere in the class and it gets appended to the constructor, so:


public class Silly {
    public Silly() {
        System.out.println("1");
    }
    
    public void neverCalled() {
        System.out.println("Wheeeeeeee!");
    }
    
    {
        System.out.println("2");
    }
}

When instantiated will print 1 and 2. You can additionally set a static block which is executed when the class is initially loaded, but I don't want to think how that will work with anonymous inner classes right now. Anyway, he did this to make sure his anonymous inner class was initialized properly.

It's only January and I think the scary-ass-code-of-the-year award has already been won =)

Read: Constructors in Anonymous Inner Classes

Topic: Offshoring in the Film World Previous Topic   Next Topic Topic: Technology strikes

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use