Results 1 to 2 of 2

Thread: Java Inner Class Source Code Separation?

  1. #1
    Join Date
    May 2007
    Location
    USA
    Posts
    373
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Java Inner Class Source Code Separation?

    Is is possible to define an inner class within a seperate source file from the outer class? If so, what is the syntax for it?
    Last edited by Trinithis; 09-29-2007 at 06:39 AM.
    Trinithis

  2. #2
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,878
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    I don't think so. Inner classes are a property of their outer class, and they're treated the same as any other property. C# has a method for splitting a single class across multiple files, but insofar as I know, Java doesn't. I would suggest, however, that if you need to do so, said class is too big. Break it up more. Inner classes are meant for convenience purposes only. If you end up with a large inner class, consider making it a normal class.

    You can always have:
    Code:
    // OuterClass.java
    
    class OuterClass {
      public static void main(String[] args) {
        System.out.println("Hello!");
    
        InnerClass ic = new InnerClass();
        ic.hello();
      }
    Code:
    // InnerClass.java
    
      class OuterClass$InnerClass {
        public void hello() {
          System.out.println("Hi!");
        }
      }
    }
    ... and cat them together.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends franšais | entiendo espa˝ol | t˘i Ýt hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

Bookmarks

Posting Permissions

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