Monday, March 7, 2016

Package in Java-4

Package in Java-4


Ways to load the class files or jar files

There are two ways to load the class files temporary and permanent.

    Temporary
        By setting the classpath in the command prompt
        By -classpath switch
    Permanent
        By setting the classpath in the environment variables
        By creating the jar file, that contains all the class files,
        and copying the jar file in the jre/lib/ext folder.

Rule: "There can be only one public class in a java source file and it must be saved
          by the public class name."

    //save as C.java otherwise Compile Time Error 
     
    class A{} 
    class B{} 
    public class C{} 

How to put two public classes in a package?

If you want to put two public classes in a package, have two java source files containing one public class, but keep the package name same. For example:

    //save as A.java 
     
    package javat; 
    public class A{} 

    //save as B.java 
    package javat; 
    public class B{}

No comments:

Post a Comment