Avoiding imports

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jrs362
    New Member
    • Oct 2006
    • 3

    Avoiding imports

    For some reason, I have to rewrite a program to avoid using import statements. I know it's possible, but cannot seem to find it in my text book. Can someone please give me a sample outline of how it's done?

    Thanks
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    Originally posted by jrs362
    For some reason, I have to rewrite a program to avoid using import statements. I know it's possible, but cannot seem to find it in my text book. Can someone please give me a sample outline of how it's done?

    Thanks
    in Java you can either specify the API package names using import statements or when you call the methods.

    For example a program using import
    Code:
    import java.util.*;
    import java.lang.*;
    
    public class Test1
    {
    public static void main(String args[])
    {
        Vector v = new Vector();
        v.addElement(Math.asin(1.0));
    }
    }
    and same program using no import statements
    Code:
    public class Test
    {
    public static void main(String args[])
    {
        java.util.Vector v = new java.util.Vector();
        v.addElement(java.lang.Math.asin(1.0));
    }
    }

    Comment

    • r035198x
      MVP
      • Sep 2006
      • 13225

      #3
      Originally posted by horace1
      in Java you can either specify the API package names using import statements or when you call the methods.

      For example a program using import
      Code:
      import java.util.*;
      import java.lang.*;
       
      public class Test1
      {
      public static void main(String args[])
      {
      Vector v = new Vector();
      v.addElement(Math.asin(1.0));
      }
      }
      and same program using no import statements
      Code:
      public class Test
      {
      public static void main(String args[])
      {
      java.util.Vector v = new java.util.Vector();
      v.addElement(java.lang.Math.asin(1.0));
      }
      }
      Perhaps not related to the question but you do not have to import anything from java.lang. The package is automatically imported for you.
      So you can just do
      v.addElement(Ma th.asin(1.0));

      Comment

      • maverick19
        New Member
        • Nov 2006
        • 25

        #4
        using fully qualified names in the body is a bad coding practice. try not to do it

        Comment

        Working...