I have a question that I did not find an answer for except the following answer that does not meet my requirements:

"Because James Gosling didn't want to"

I know that Java can have interfaces (only pure virtual functions, no attributes), but it is not the exact same thing as class definitions.

  • 14
    What is the reason you want them?
    – user1249
    Commented Nov 10, 2011 at 21:30
  • 19
    To have more time for swordfights ;)
    – dan04
    Commented Nov 11, 2011 at 0:48
  • 7
    I suppose most C++ developers would love to get rid of the 40 year old text replacement engine duplicating hundreds of kLoC for every cpp file, leading to C++' long compile times. In fact a proper module system was contemplated for C++11, but dropped due to lack of time. I suppose it will come up again, though.
    – sbi
    Commented Nov 11, 2011 at 9:11
  • I suppose it will come up again, though. In fact WG21 (the ISO C++ working group) has a study group solely for the purpose of evaluating/developing the "modules" concept further: SG2 "Modules". Too bad it's current status is dormant.
    – Max Truxa
    Commented Jun 12, 2015 at 15:57

6 Answers 6


The following answer that does not meet my requirements: "Because James Gosling didn't want to."

That's the right answer, though. The language design team (Gosling, Sheridan, Naughton, later Bill Joy, Ken Arnold, etc.) decided headers caused more problems than they solved. So they designed them out, and demonstrated that they could create a perfectly useful language without the need for them.

From Section 2.2.1 of the Java Language Environment white paper:

Source code written in Java is simple. There is no preprocessor, no #define and related capabilities, no typedef, and absent those features, no longer any need for header files. Instead of header files, Java language source files provide the definitions of other classes and their methods.

Redundant definitions, keeping files in sync, conflicting definitions, hidden definitions--none of these occur in Java, because you don't have headers. If you want to see a bare class definition, you can generate one from a .java file directly--e.g. most IDEs will show you the structure of a class in a sidebar, which amounts to the same thing.

  • 6
    Thanks for your answer, then, the headers caused more problems because of that: Redundant definitions, keeping files in sync, conflicting definitions, hidden definitions. Is that why it was not allowed? Commented Nov 10, 2011 at 18:28
  • 2
    Note that there is a lot of talks about the next C++ Commitee meetings because they will consider a new "Module" system that would be a simpler and more efficient system than including (with some similarities with java, C# etc. packages) but still retro-compatible with includes. That's to say that a better compilation system can, at least can in theory, be used to make C++ compilation better/more efficient. Gosling was right I guess, and C++ have to find a way to fix the include system anyway.
    – Klaim
    Commented Nov 10, 2011 at 22:31
  • 5
    It's not perfectly usable. The Java build systems are unable to determine which files must be recompiled after a code change. An IDE will determine which files require a code change, but not which need recompilation. If a method signature changes, but the change is code-compatible with the old signature (e.g. changing an argument type from float to double), a clean build is required to prevent MethodNotFoundException. Commented Nov 11, 2011 at 0:51
  • 1
    @kevin: However it's usually possible to just rebuild everything without too much cost. Unlike C++ (but like nearly every other compiled language on earth) Java just doesn't take that long to compile that partial compilation is a very worthwhile optimization of your development workflow. Commented Jan 16, 2012 at 15:18
  • 1
    @Donal: It's true that Java compiles pretty quickly, but I hate guessing whether or not I should do a full recompile. Builds should just work, every time. Commented Jan 17, 2012 at 16:39

There is no real need in C++ to have the class definitions and declarations in separate files. It just means that you could, at least back in the C days, do the parse in a single top-bottom scan of the code. On machines without random access storage this was a big deal!

Having headers also allowed you to publish the interface to your code library by supplying the header without needing to reveal the source code. Unfortunately in C++ you also have to reveal the private data members which has led to solutions like the horror of pimpl.

There have been attempts to make a C++ environment where everything was stored in a database type structure and there are no files but it didn't catch on.

  • I know that, but at least you can do it in C++ not in Java; that was my main inquiry. Thanks for the answer. Commented Nov 10, 2011 at 18:20

Because of the DRY principle. In Java, the information needed to use classes in a package (or class) are contained inside the .class file. Creating separate header files that contain the same information would involve repeating it in two places.

  • unfortunately, you often want to repeat it - think of wsdl files, idl files etc. One describes the interface you can use, and another file contains the implementation. C++ headers are (poor) interface definitions.
    – gbjbaanb
    Commented Jan 16, 2012 at 22:57

In every language - there are two stages for creating final binary code - compiling and linking (of course, there is loading but that has not much impact here). At the time of compiling one only needs to put hooks (the specification of functions that will be called) at appropriate place. Linker actually joins them when both real code is available. So far there is no difference between C++ and Java.

There is, however, a need for C++ to have declaration and definition separate. If you keep the implementation in header, and if the header file changes, the code which is linked with it needs to get recompiled. Where as if the definition is in separate file, the code only needs to get re-linked.

Understand that C++ does have option for having static linking which implies that the object code is fixed along with the calling application. Please note that both in C and C++, it is not invalid to have programming in header file or even do #include . it only means that you need to bother about how linking happens with these object files.

The situation in Java is very different. Each class file is compiled with .class file. Indeed, the need for caller class function compilation which is served as a header section in .class file. However, in Java final linking is done only inside the Runtime (the virtual machine) only with given that specification of class file's byte code.

See this and this


Effectively the interfaces and includes are the headers; the definitions are therefore identical to the binaries, and can't be out of synch. This is one of the best design decisions in Java, but it is a minor annoyance that there's no way to bundle these declarations for compactness and consistency.


One good reason to have includes is to separate code you may want to reuse (such as common definitions) from the code that's specific to a given project. Java wants you to specify only one class or interface per file and that mostly reduces the need for included headers - for you will nave the shared parts already in their own files.

Also, compilers and build systems may want to cache precompiled headers to avoid parsing them more than once.

  • 1
    I suppose you could store the interfaces in a shared project and then implement them in independent projects Commented May 5, 2018 at 2:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.