why java does not need to have header files like C and C++ ?
in java RTTI enables the programmer to identify the interface of classes and methods, is this the only reason? or what else?
thanks in advance...
The java compiler (notably "javac") is able to read compiled code; compiled
code is not supposed to lie; given a compiled class (example):
-
package foo;
-
Class Foo {
-
public int bar;
-
// possibly more stuff here ...
-
}
-
When you reference this class (example):
-
class Bar {
-
foo.Foo foo= new foo.Foo();
-
void someMethod() { foo.bar= 42; }
-
}
-
When you compile class Bar the compiler searches for a compiled class foo.Foo
(package and class name) and checks whether or not there is some 'bar' member
in there. If there is, it continues, if there isn't it starts to whine at you about a
member 'bar' not being available in a 'foo.Foo' or even worse, if it can't find a
compiled 'foo.Foo' it'll whine about that.
You can help the compiler a bit by 'importing' the package and optionally the class
itself:
-
import foo.Foo; // or 'import foo.*;'
-
class Bar {
-
Foo foo= new foo.Foo();
-
void someMethod() { foo.bar= 42; }
-
}
-
But still the compiler wants to see a compiled foo.Foo class when it has to compile
this Bar class.
You can even give a path to where the source files (the .java files) exist to the
compiler in which case it does something like this: 1) look for a compiled foo.Foo
class and if it isn't found but a Foo.java source file is found, compile that and then
continue. If nothing was found start whining about it. This mechanism effectively
cancels the need for #include'd files and is a bit more trustworthy.
kind regards,
Jos