Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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.

share|improve this question
14  
What is the reason you want them? –  user1249 Nov 10 '11 at 21:30
17  
To have more time for swordfights ;) –  dan04 Nov 11 '11 at 0:48
6  
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 Nov 11 '11 at 9:11

6 Answers 6

up vote 43 down vote accepted

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.

share|improve this answer
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? –  CoachNono Nov 10 '11 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 Nov 10 '11 at 22:31
4  
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. –  kevin cline Nov 11 '11 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. –  Donal Fellows Jan 16 '12 at 15:18
1  
So if they could "create a perfectly useful language without the need for them", why did they release Java instead? :-) –  Jerry Coffin Feb 16 '12 at 17:27

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.

share|improve this answer
    
I know that, but at least you can do it in C++ not in Java; that was my main inquiry. Thanks for the answer. –  CoachNono Nov 10 '11 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.

share|improve this answer
    
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 Jan 16 '12 at 22:57

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.

share|improve this answer

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

share|improve this answer

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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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