Tuesday, 28 June 2016

Source file declaration rules

As the last part of this section let's now look into the source file declaration rules. These rules are essential when declaring classes, import statements andpackage statements in a source file.
  • There can be only one public class per source file.
  • A source file can have multiple non public classes.
  • The public class name should be the name of the source file as well which should be appended by .java at the end. For example: the class name is public class Employee{} then the source file should be as Employee.java.
  • If the class is defined inside a package, then the package statement should be the first statement in the source file.
  • If import statements are present then they must be written between the package statement and the class declaration. If there are no package statements then the import statement should be the first line in the source file.
  • Import and package statements will imply to all the classes present in the source file. It is not possible to declare different import and/or package statements to different classes in the source file.
Classes have several access levels and there are different types of classes; abstract classes, final classes, etc. we will be explaining about all these in the access modifiers chapter.
Apart from the above mentioned types of classes, Java also has some special classes called Inner classes and Anonymous classes

No comments:

Post a Comment