June 13, 2014

Java Packages

In Java, import is simply used by the compiler to let you name your classes by their unqualified name, let's say String instead of java.lang.String. You don't really need to import java.lang.* because the compiler does it by default. However this mechanism is just to save you some typing. Types in Java are fully qualified class names, so a String is really a java.lang.String object when the code is run. Packages are intended to prevent name clashes and allow two classes to have the same simple name, instead of relying on the old C convention of prefixing types like this. java_lang_String. This is called namespacing.

Oracle includes 209 packages in the Java SE 7 API. You can either do a explicit import or a implicit import.


import java.util.ArrayList; - explicit
import java.util.*; - implicit



Since import is straight forward let me show you how to do import static. import static  is useful whenever you are trying to access static data members of a class.

1. There are two classes called Car and Vehicle where we are trying to access static variable BUGATTI without instantiating an object of Car in main method of vehicle.

package com.george.cars;//0 or 1 package name only
public class Car {
public static final int BUGATTI = 1;
public static final int GOLF = 2;
public static final int JETTA = 2;
}

package com.george.vehicles;//0 or 1 package name only
import com.george.cars.Car;//here importing normally
public class Vehicle {
public Vehicle(int type) {
if (type == BUGATTI) {
System.out.println("Hello bugatti");
}
}
public static void main(String[] args) {
Vehicle v = new Vehicle(BUGATTI);
}
}


But the above code throws compilation errors because the static variable BUGATTI is not visible from the other package as static is not used. But the following is correct,

package com.george.vehicles;//0 or 1 package name only

//import com.george.cars.Car;//no need to import
public class Vehicle {
public Vehicle(int type) {
if (type == Car.BUGATTI) {
System.out.println("Hello bugatti");
}
}
public static void main(String[] args) {
Vehicle v = new Vehicle(Car.BUGATTI);
}
}

This is because, we have told the compiler that we use a variable BUGATTI from the car class. But as you can see, each time we have to repeat Car.BUGATTI which is annoying and involves more typing so alternative approach is shown below,

package com.george.vehicles;//0 or 1 package name only

import static com.george.cars.Car.BUGATTI;
public class Vehicle {
public Vehicle(int type) {
if (type == BUGATTI) {
System.out.println("Hello bugatti");
}
}
public static void main(String[] args) {
Vehicle v = new Vehicle(BUGATTI);
}
}


Also note that we can invoke nested static classes from another project like this import static george.logger.Timber.DebugTree; where DebugTree is a static class


Widely used Java packages are,

1. java.util.*; - collections, date/time



















2. java.awt.*; - to paint basic graphics & images













3. java.io.*; - data streams I/O


















4. java.net.*; - networking















5. javax.swing.*; - lightweight components for GUI
The prefix java is commonly used for the core packages. The prefix javax is commonly used for packages that comprise Java standard extensions. javafx will replace javax.swing



















Links: how-java-import-works
Note: Two packages are implicitly imported into a class. One is the java.lang package and the other is the default package in which we are working. 

No comments:

Post a Comment