java source file structure


                             Java source file structure

1. Can a java program contain multiple classes?
         Yes, it can. However, there can be only one public class per .java file.
         A public class must be implemented in a file with the same name as the class.
         A single file can contain one public and optionally some default classes.
         The purpose of including multiple classes in one source file is to bundle related support functionality together.
         Note that it is always ok not to do this -- the only effect is on the readability (or not) of your code.
2. If a java program contains multiple classes, is it allowed to take main method in every class?
         Yes u can have more classes that contain the main().
         And you can chose to execute one class or another class.
         But you can’t have more than one main method within same class.
3. If a java program contains multiple classes, then how many classes can be declared as public?
         There can  be only one public class per .java file
4. What is the purpose of import statement? If we are using fully qualified name is it required to use import statement?
         The import statement in Java allows to refer classes which are declared in other packages to be accessed without referring to the full package name.
         Classes from the java.lang package are automatically imported, so you do not need to explicitly do this, to refer to String, for example.
         You do not need any import statement if you are willing to use fully qualified name always.
5. Is it possible to use fully qualified name and import statement simultaneously?
         Yes ,it is possible
6. What is the difference between import java.util.* & import  java.util.ArrayList?
         By importing java.util.* we can access all the class and interfaces   present in util package. But sub packages cannot be accessed.
         By importing java.util.ArrayList we can access only the ArrayList class.
7. Which packages are not required to import to in java?
         Classes from the java.lang package are automatically imported
8. Whenever we are importing a package is sub package classes are available?
         No, sub packages cannot be accessed
9. What is the difference between general import and static import?
         Static imports let you avoid qualifying static members with class names.
         Once the static member is imported then you can use it in your code without the class name prefix.
         The main difference is Readablity, Constant.USER_NAME is less readable when compared to USER_NAME.
         Static import can make your program more readable, by removing the boilerplate of repetition of class names.

10. Explain the new features introduced in java 1.5 versions?
         Generics
         Generics provide a way to create and use type safe data structures.
         This means that no longer do you have to create a List of the basic Objects then typecast every time you pull stuff out! You can declare your list to automatically typecast the stuff inside
         For/in loop
         Autoboxing/Unboxing
         Typesafe Enums
         Enums are just magic classes to help prevent the methodless-interface antipattern.
         They let you make classes that will enumerate values, but also keep the types specific. Before, we could simulate enums with a bunch of static final int variables or something. The problem with those is that you could confuse any int with one of the constants. With enumerations, only the values in the enum are valid.
         Varargs
         Static Import
         Annotations (Metadata)
11. What is the purpose of package statement?
         Packages in Java are simply a mechanism used to organize classes and prevent class name collisions
         Java packages can be stored in compressed files called JAR files, allowing classes to download faster as a group rather than one at a time.
         Programmers also typically use packages to organize classes belonging to the same category or providing similar functionality.
12. Is it possible to take more than one package statement?
         There can be only one package statement in each source file.
13. Is it possible to take package statement anywhere?
         No ,The package statement (ex: package graphics;) must be the first line in the source file.
14. What is the correct sequence of statements of java program?
         package declarations, import statements, then class definitions.
15. Is empty source file valid java program?
         Yes, empty file is valid Java source file
         Java-source file with zero class-declarations will not result in any .class files, so it's not really handled by the JVM at all