Programmer's Rules


Rules of declaring classes and enums in Java

Job interview question for Java developers

Javajob interviewOCA

1. There are 4 access levels: public, protected, (default) and private but 3 access modifiers: public, protected and private. Do not make a mistake.

2. Inner classes can have all 4 access levels but outer classes only 2: public and default.

3. Inside a *.java file there can be only 1 public class but many non public classes;

4. protected access level “is seen” inside the same package as default plus the protected is seen in the inheritance relation.

5. If a *.java file contains a public class, the class name must be the same as the name of the file;

6. There can be only one package statement;

7. The package statement must be the first one inside a file (if the statement exists).

8. package and import apply to all classes in the file. If there is no package statement, import statements are the first statements in the source file.

9. classes may have non access modifiers: final, abstract lub strictfp;

10. for the exam you should only know that strictfp is a keyword and can exists in a class or method declaration but never in a variable declaration;

11. abstract and final never exist together;

12. The order of objects and classes initiation:

a) class initiation – parent’s and child’s static blocks (i.e. static {})),

b) parent objects initiation – blocks {} and then constructor,

c) children objects initiation – blocks {} and then constructor;

13. enum types (enum) may be declared inside a class or outside a class but never inside a method (not inside a constructor either);

14. a semicolon is optional after the enum declaration;

15. enum that is defined outside a class cannot be signed as static, final, abstract, protected or private.

Tutorial statistics