Home Javatutorials Many Public Classes in One File - Java Tutorials



Many Public Classes in One File - Java Tutorials
Posted on: April 18, 2011 at 12:00 AM
This page discusses - Many Public Classes in One File - Java Tutorials

Many Public Classes in One File

2003-10-13 The Java Specialists' Newsletter [Issue 080] - Many Public Classes in One File

Author: Dr. Heinz M. Kabutz

If you are reading this, and have not subscribed, please consider doing it now by going to our subscribe page. You can subscribe either via email or RSS.

Welcome to the 80th edition of The Java(tm) Specialists' Newsletter. There is a saying in the English language: It never rains, but it pours. This means that either I am unbelievably busy, or I have time to write several newsletters at once. So, at the moment the sun is shining and I have the luxury of being able to relax with my hobby of writing newsletters.

The idea in this newsletter was prompted by a professor who attended my Java classes in China. I was talking about Java only allowing one public class per file, and he asked: "What about inner classes?" The resulting discussion ended up in this newsletter. Technically, it is possible to have lots of public static inner classes and use them as normal public classes, but I would strongly recommend against that.

Advert Alert: Would your company like to encourage your developers to use Design Patterns but you do not have the budget to send them all on a course? Have you considered licensing our course for internal use? We would love to hear from you if you would like to achieve better communication within your development team and improve the skills of your programmers.

We are getting more organised. We have now got a schedule of when our courses are happening. I have resisted doing this for five years, because often these schedules are closer to fiction than reality. We will try to stick to these dates, but we are flexible enough to move them if you need us to.

Many Public Classes in One File

Some of the most common complaints that I hear about Java are these:

  1. Java is slow.
  2. You cannot make an "exe".
  3. Every Java class has to be in a separate file.

I sometimes struggle to take these complaints seriously. Here are my standard answers:

  1. The speed of execution depends greatly on the skill of the developer. I have heard of real-world applications where the Java application was faster than an equivalent C++ application. For number crunching, you will probably do better with C++, but for general applications, the advantages of Java outweigh the slight performance degradation that you may experience.
  2. There are several compilers available that will produce an "exe" from Java classes. Some of these are fairly powerful. In a future newsletter we are planning to review some of them.
  3. Usually my answer here is that it makes source control easier when you have a separate file for each class. As described in my newsletter "Once Upon an Oak", a reason for this restriction is to make the compilation process faster. This newsletter will demonstrate an approach where you can have several public classes in one .java file.

Before I show you this approach, please understand that I do not endorse writing classes in this way. I prefer using one file per class, even for non-public classes. I once had to work on a class that had about one hundred inner classes. It was a nightmare.

We start by defining package com.maxoft.tjsn which contains class All. This class contains all the classes as public static inner classes.

package com.maxoft.tjsn;

public class All {
  public static class A {
    public void f() {}
  }
  public static class B {
    public void g() {}
  }
  public static class C {
    public void h() {}
  }
  public static class D extends A {
    public void f() {}
  }
  public static class E {
    public void jump() {}
  }
  public static class F {
    private final E e = new E();
    public void skip() {
      e.jump();
    }
  }
}
  

It is important that the class is in a package, as we will see just now. Next we show the first approach of how we could now use classes A,B,C,etc.

import com.maxoft.tjsn.All;

public class AllTest1 {
  public static void main(String[] args) {
    All.A a = new All.A();
    All.B b = new All.B();
    All.C c = new All.C();
    All.D d = new All.D();
    All.E e = new All.E();
    All.F f = new All.F();
  }
}
  

Admittedly, this is ugly. However, there is another approach. We can import the inner classes directly, like so:

import com.maxoft.tjsn.All.*;

public class AllTest2 {
  public static void main(String[] args) {
    A a = new A();
    B b = new B();
    C c = new C();
    D d = new D();
    E e = new E();
    F f = new F();
  }
}
  

This is now a lot prettier than saying All.A and All.B. I think it only works if the class is in a package. This should not be a restriction since classes should be in packages anyway.

But, rather don't do this...

Here are some reasons why you should rather not do this: First, it is going to result in large source files. Second, all members of the classes will effectively only have package access protection. This can easily result in spaghetti code that would make an Italian Mama proud. Third, most IDEs will struggle to support this well. Fourth, I seem to recall that some compilers cannot compile importing of inner classes.

Kind regards

Heinz

This material from The Java(tm) Specialists' Newsletter by Maximum Solutions (South Africa). Please contact Maximum Solutions for more information.

Related Tags for Many Public Classes in One File - Java Tutorials:


More Tutorials from this section

Ask Questions?    Discuss: Many Public Classes in One File - Java Tutorials  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.