Monday, March 2, 2009

import package static import

Java Source Structure:

  • A java source may contain any number of classes. But among these classes we can declare almost class as the public.
  • If there is a public class the name of public class and the name of the program must be marked
  • If there is no public class, then you are allowed to use my class name as the name of the file.
Import Statements:
Class sample
{
public static void main(String arg[])
{
ArrayList a=new ArrayList();
}
cannot resolve symbol
symbol:class ArrayList
class sample
public static void main (String arg[])
{
ArrayList();
}
}
cannot resolve symbol
symbol : method ArrayList();

java.util. ArrayList a = new java.util.ArrayList

Explicit class import: import java util. ArrayList – improves the readability of the code.
implicit class import / wildcard import : import java. Util.*

CASE 1:
Class sample extends java.rmi. UniCastRemoteObject //Complies fine
Note:- if we are using fully qualified name, no need to go for import statements.
CASE 2:
Which of the following is a valid java import statement?
a) import java.util.*; // valid
b) import java.util.ArrayList // valid
c) import java.util; // invalid
d) import java. Arraylist.*; //invalid
CASE 3:
By increasing number of imports, compile time will increase. But there is no effect on run time or execution time.
CASE 4:
Lang package and default package (current worling directory) are by default available to our program. No need to go for explicot or implicot imports.
The compiler resolves the import statements on the following order:
a) Explicit class impost
b) The classes present on current working directory
c) Implicit class imports.

With in a java file any number of implicit are allowed.
Static Imports:
  • Instead of refreshing static numbers by class name every time, we can write static import statement and directly we will refer that member without class name
  • According to sun people, static imports improve readability of the program. But exports are not accepting this.
WITHOUT STATIC IMPORTS:

Class sample
Public static void main (String[] args)
{
System.out.println(Math.max(10,20)); // 20
System.out.println (Math.PI); // 3.14.15…
System.out.println(Byte.MAX_VALUE); //127

WITH STATIC IMPORT:

Import static java.lang. math.*;
Class sample
{
Public static void main (string{} args}
{
System.out.println (max(10,20);
System.out.println (PI)
System.out.println (MAX_VALUE);
}
import static java.lang.Byte. MAX_VALUE;


Ambiguity in the import statements:
import java.util.*;
import java.sql.*;
class Sample
{
public void main(String arg[])
{
Date d=new Date();
}
}
error:reference to Date is ambiguous.
Date class is in both util and sql packages.
So java.util.Date d=new java.util.Date();
In this situation ,import statement is not helpful. we should go for fully qualified name.the same problem will come for the list also because List is available in both java.util and java.awt packages.
Ambiguity in the import statements:
import static java.lang.Byte.*;
import static java.lang.Integer.*;
class Sample
{
public void main(String arg[])
{
System.out.println(MAX_VALUE);
}
}
error : reference to MAX_VALUE is ambiguous.
out,in,error are the static objects available in system.
import static java.lang.System.out;
then out.println(Integer. MAX_VALUE);
If we are using import pack1.* all the classes which are present in package 1 only will be available in the program but not sub package classes.

Package Statements:
A package statement in java is an encapshlation (single module)mechanisam to group related classes and interfaces into a single module.The perpose of package statement is to resolve naming conflicts.It is real time convension to name the package names as use their internet domain names in reverse.
Example: com.Airtel ,com.javaexamples4u.
Example:
package com.javaexamples4u;
Class Sample
{
public static void main(String arg[])
{
System.out.println(“hello”);
}
}
E:/java:>javac Sample.java
The generated .class files is placed in the present default directory.
E;/java:>javac –d . Sample.java
-d indicates the destination to keep the .class file.
E:/java:>javac –d C:/Sample.java
.class file is stored in C:/com/javaexamples4u.
Running:java com.javaexamples4u.Sample

In the java source file only one package declaration is allowed.If you are keeping more then one package declaration ,we will get a compile time error .
The package declaration statement must be the first non-comment statement in any java file
Java source file structure :
package statement
import statement
class/interface/enum declaration
order is very important .other wise compile time error.

0 comments:

Post a Comment