Monday, March 24, 2014

FIRST PROGRAM , Part 4 - Some Rules To Consider For Writting Java Source(.java) File.

In First Program series, till now i have explained how to write , compile and run .java file. Here i will tell about some rules which we should consider before writing and saving the Java source file. (just consider these rules , in later posts i will explain about public, private, protected, default).
  1. There can be only one top level public either class or interface in any java compilation unit ( .java source file ).
  2. If there is a public class in a file, the name of the file must match the name of the public class.
  3. There can be any number of default classes/interfaces per src file.
  4. A file can have more than one nonpublic class
  5. A file can be without public class ( but top level class can't be private or protected)
  6. Files with no public classes can have a name that does not match any of the classes in the file.
  7. Comments can appear at the beginning or end of any line in the source code file; they are independent of any of the position rules.
  8. If there is main method and public class then main method should be in that public class.
  9. If there is no public class and no main method then any valid name which may or may not be matching with the class/interface names in the file will be allowed.
  10. If the class is part of a package, the package statement must be the first line in the source code file, before any import statements that may be present.
  11. If there are import statements, they must go between the package statement (if there is one) and the class declaration. If there isn't a package statement, then the import statement(s) must be the first line(s) in the source code file. If there are no package or import statements, the class declaration must be the first line in the source code file.
  12. Import and package statements apply to all classes within a source code file.In other words, there's no way to declare multiple classes in a file and have them in different packages, or use different imports.
Some curious mind may be thinking on why Java source file can have one only public class? (for better learning we should avoid such questions at this stage)

Here, first i would like to share the approach to answer these kind of questions. 
To answer these questions , try to write what has been asked ,i mean, if it has been asked can we have more than one public class in single src file then to answer this do practicals and write a src file with two public classes and see yourself what the problems you will be facing ( this approach has always helped me to learn in better way). 
Anyway, here is my simple answer(may not be at technical advance level):
  • If two public classes were allowed, we should give the file two names which is terribly meaningless to file system.
  • If two public classes allowed, we should have two main methods which is terribly meaningless to java.
  • I think the above points are forced by compiler to make the job of both compiler and jvm to find particular java source file or class file easy and quick for the compilation/loading/linking. Java has such built in restrictions which developers should follow to have better programming.

No comments:

Post a Comment