03 - Java Package
03 - Java Package
There are three ways to access the package from outside the package.
1. import package.*;
2. import package.classname;
3. fully qualified name.
If you use package.* then all the classes and interfaces If you import package.classname then only declared
of this package will be accessible but not sub class of this package will be accessible.
packages.
If you use fully qualified name then only declared class of this package will be
accessible. Now there is no need to import. But you need to use fully qualified name
every time when you are accessing the class or interface.
It is generally used when two packages have same class name e.g. java.util and
java.sql packages contain Date class.
Java Static Import
The static import feature of Java 5 facilitate the java
programmer to access any static member of a class directly.
There is no need to qualify it by the class name.
Advantage of static import:
Less coding is required if you have access any static
member of a class oftenly.
Disadvantage of static import:
If you overuse the static import feature, it makes the
program unreadable and unmaintainable.
What is the difference between import and static import?
The import allows the java programmer to access classes of a package without package
qualification whereas the static import feature allows to access the static
members of a class without the class qualification. The import provides
accessibility to classes and interface whereas static import provides
accessibility to static members of the class.
To find the length of S in given code, what you have to write in place of Ans??
1. Here, S is static variable of type
String present in Test class
2. So, static variable is access using
class_name.static_variable_name as
Test.S
3. To find length of static variable S,
length() method is used of class String
where S is an object and we know
object can access method as
S.length()
Same concept is used in System.out.println() as:
1) Compiler by default imports java.lang
package which includes System class.
2) out is a (public static final) variable of type PrintStream
declared in the System class. PrintStream is another class.
Refer green Arrows for access with respect to Actor class in below E.g.