Import specific classes or whole packages?

Since I began Java programming a few years ago, I have always used import functions to import whole packag.
e.g. import java.util.* instead of java.util.ArrayList
I have noticed that my IDE of choice (Eclipse) and many examples on the web use specific import statements instead of whole-package ones. Is there any advantage to this, performance or otherwise?
thanks,
Andrew

nice advice, jackasspublic class Fred extends java.awt.Frame {
public void paint(java.awt.Graphics g) {
int width = getWidth();
int height = getHeight();
g.setColor(java.awt.Color.white);
g.fillRect(0, 0, width, height);
g.setColor(java.awt.Color.black);
java.awt.FontMetrics fm = g.getFontMetrics();
int sWidth = fm.stringWidth("Suck it");
int sHeight = fm.getHeight();
int x = (width - sWidth)/2;
int y = (height - sHeight)/2;
g.drawString("Suck it", x, y);
public static void main(java.lang.String[] args) {
Fred fred = new Fred();
fred.addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent e) {
java.lang.System.exit(0);
fred.setSize(new java.awt.Dimension(400, 400));
fred.setVisible(true);

Similar Messages

  • Import the classes in the package java.util

    I haven't taken a class in two years so I have ideas but am unsure of myself, and I most likely am way off base, but is this how you import the classes in the package java.util
    import java.util.*;
    I would appreciate anyone's help. I have a lot of questions.
    Thanks,
    rp

    My assignment is below so that you will know exactly what I am asking. I am being asked to create an instance of LinkedList. I know that this is a part of the java.util. So when you create an instance of LinkedList would that be similar to instance variables? or am I misunderstanding something. I have several ideas, so I will start with the first one.
    LinkList();
    I really appreciate your taking the time to answer my questions to help me to rebuild my confidence in doing this.
    Thanks,
    // 2. import the classes in the package java.util
    public class MyProgram
         public static void main(String [] args)
              // 3. Create an instance of LinkedList
              // 4. add 5 entries to the list so that it contains
              //     Ann, Bart, Carl, Dirk, Zak
              // 5. display the list on one line using a loop and ADT list methods
              System.out.println("\n\n3. ");
              // 6. display the list using one println
              System.out.println("\n\n4. ");
              // 7. create a ListIterator object for the list
              // 8. display the list on one line using the iterator
              System.out.println("\n\n6. ");
              // 9. restore the iterator to the first item in the list
              // 10. retrieve and display the first item in the iteration,
              //      then advance the iterator to the next item
              System.out.println("\n\n8. ");
              // 11. advance the iterator to the end of the list without displaying anything;
              //      do not use the length of the list
              // 12. display the last item in the list
              System.out.println("\n\n10. ");
              // 13. move back to the beginning of the list without displaying anything;
              //      do not use the length of the list
              // 14. display the first item in the list
              System.out.println("\n\n12. ");
              // 15. advance the iterator to the end of the list without displaying anything;
              // 16. advance the iterator - what happens?
              System.out.println("\n\n14. ");
              // 17. advance the iterator within a try block; catch the exception, display a message,,
              //      and set the iterator back to the beginning of the list.
              System.out.println("\n\n15. ");
              // 18. what does nextIndex and previousIndex return?
              System.out.println("\n\n16. ");
              // 19. move the iterator to the third item in the list; if you were to
              //      execute next(), the third item would be returned and the iterator
              //      would advance to the 4th item.
              System.out.println("\n\n17. ");
              // 20. add the string "New" to the list; where is it inserted relative to
              //      the current item in the iteration?
              System.out.println("\n\n18. ");
              // 21. remove the current item; what happens?
              System.out.println("\n\n19. ");
              // 22. display the current item in the list without advancing the iteration
              //      in 2 ways, as follows:
              // 22A - using only iterator methods
              System.out.println("\n\n20A. ");
              // 22B using an iterator method and list methods
              System.out.println("\n\n20B. ");
              // 23. advance the iterator and remove the current item; which one is removed?
              System.out.println("\n\n21. ");
              // 24. move the iterator back and remove the current item; which one is removed?
              System.out.println("\n\n22. ");
              // 25. move the iterator to the first item in the list and change it to "First"
              System.out.println("\n\n23. ");
         } // end main
    } // end MyProgram

  • Why import specific classes?

    When you import a package or a class what is the point in importing a specifc class, instead of using the * to just import the whole package?
    i.e. import java.lang.*;
    Does importing an entire package slow the program down, or affect perfromance in another way?
    thanks
    mike

    No, it doesn't affect the runtime performance in any way.
    You don't even need to "import" anything. You could just write "java.awt.event.MouseListener" every time you needed a MouseListener. Importing is there only to spare your fingers and wrists and save some time.

  • How to import a class from a package at the same level

    Hello friends,
    i have a class as Plaf.java
    as
    package org.vaibhav.swing.plaf;
    import java.awt.*;
    import javax.swing.*;
    public class Plaf {
    other code
    }as you see, this is in org.vaibhav.swing.plaf package.
    I have one more class as
    package org.vaibhav.swing.frames;
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class StartFrame extends JFrame implements ActionListener {
    other code
    }and this class is in package org.vaibhav.swing.frames
    Please hlp me how to use the class Plaf in StartFrame.java. Rather what import statement should i use in StartFrame.java.
    please help.
    thank you
    Message was edited by:
    vaibhavpingle

    but it then gives me this error
    StartFrame.java:11: package org.vaibhav.swing.plaf
    does not exist
    import org.vaibhav.swing.plaf.*;
    Have you first compiled Plaf.java and saved it in this directory strucuture
    /org/vaibhav/swing/plaf/
    And also have you set your classpath to the parent directory of org folder.
    Message was edited by:
    qUesT_foR_knOwLeDge

  • Import java class from model package

    I have a java class in model package.I create a managed bean in the view package and I want to import the first java class with "import model.class1" but this error "import model.class1 not found" is shown.What should I do?

    Hi,
    Why are you importing the Model classes to your backing bean? You should not be doing that. Instead, expose the method in model as client interface (whichever you want to call in the backing bean), create the method binding in your pagedef. Then, from the backing bean, you can execute the method programatically.
    -Arun

  • Import individual classes or entire package?

    Hi All,
    This is just a general curiosity question but I was wondering what the best way is to import mulitple classes from the same package. i.e. is it better to do something like this:
    import java.awt.Color;
    import java.awt.Dimension;
    import java.awt.GridBagConstraints;
    import java.awt.GridBagLayout;
    import java.awt.Insets;
    import java.awt.Toolkit;
    import java.awt.event.ActionEvent;
    import java.awt.event.KeyEvent;or this:
    import java.awt.*;Is the compiler smart enough to only import the needed classes as would be the case in 'import java.awt.*;'?

    This document had some pretty good reason not to use the .* import:
    http://wwws.sun.com/software/sundev/whitepapers/java-style.pdf
    - The most important reason is that someone can later add a new unexpected class file to the same package
    that you are importing. This new class can conflict with a type you are using from another package,
    thereby turning a previously correct program into an incorrect one without touching the program
    itself.
    - Explicit class imports clearly convey to a reader the exact classes that are being used (and which classes
    are not being used).
    - Explicit class imports provide better compile performance. While type-import-on-demand declarations
    are convenient for the programmer and save a little bit of time initially, this time is paid for in
    increased compile time every time the file is compiled.

  • Im using BlueJ, I want  to import a class from a path I set.

    Hi, I'm using BlueJ and I want to import a class from a path I set.
    Like this
    import "C:\myfolder\*";
    Instead of like this
    import java.thing.*;
    Any ideas why the above line is not working for me?

    The import statement takes a class name or package name + .*, not a file name. It has nothing to do with files.
    import package.name.ClassName; // to import a class
    import package.name.*; // to import all classes in the package named "package.name"
    You need to properly specify your classpath at compile-time as well as run-time.

  • How to import a class not in the package from a package

    how to import a class not in the package from a package?

    http://java.sun.com/docs/books/tutorial/java/interpack/usepkgs.html

  • Trying to page import directive in a JSP  access a class in default package

    I am trying to import a class the is in my default package directory for Tomcat 4.1.18 (context-root\web-inf\classes\*.java). I am getting the following error
    This is error using Tomcat 4.1.18
    org.apache.jasper.JasperException:Unable to compile class for JSP
    C:\Program Files\Tomcat4.1\work\Standalone\localhost\csc297\HTML\SearchByName_jsp.java:9: '.' expected
    import Product;
    C:\Program Files\Tomcat4.1\work\Standalone\localhost\csc297\HTML\SearchByName_jsp.java:10: '.' expected
    import Category;
    This is error using Tomcat 4.0.6
    org.apache.jasper.JasperException: Unable to compile class for JSPNote: sun.tools.javac.Main has been deprecated.
    C:\forte4j\tomcat401\work\localhost\C_3A_5Cforte4j_5Ctomcat401_5Cwebapps_5Ccsc297\HTML\SearchByName$jsp.java:5: Class Product not found in import.
    import Product;
    ^
    C:\forte4j\tomcat401\work\localhost\C_3A_5Cforte4j_5Ctomcat401_5Cwebapps_5Ccsc297\HTML\SearchByName$jsp.java:6: Class Category not found in import.
    import Category;
    Here is snippet of code from my JSP:
    <%@ page import= "java.util.*"%>
    <%@ page import= "java.text.DecimalFormat"%>
    <%@ page import="Product"%>
    <%@ page import="Category"%>
    Remember these classes are in my default package of my context-root
    Any ideas would greatly appreciated

    Weird error. Never seen this one before.
    Try packaging your classes eg:package myClasses;and import them:<%@page import="myClasses.*"%>Should fix the problem.
    Anthony

  • How to import a class, which is not in a package?

    Hi all,
    I have the following problem:
    A class A is not in a package. In another class B, which is in a package (let's say package X) I want to use the class A. Without importing the class A, I get the error "Cannot resolve symbol".
    Using the import statement import A; gives the error ". expected".
    How can I import that class?
    Best wishes to all ...
    Heiko

    > I have the following problem:
    A class A is not in a package. In another class B,
    which is in a package (let's say package X) I want to
    use the class A. Without importing the class A, I get
    the error "Cannot resolve symbol".
    I think not putting a class in a package is a bad practice. Avoid that.

  • Importing class from default package of a JAR

    I want to create objects of a class contained in a JAR. The JAR has many classes in numerous packages, however the class that I want to use is in the default package. I'm using eclipse and have added the JAR to the build path, but eclipse complains that the class name cannot be resolved. Is there some way to specify an import statement to look in the default package of a particular JAR? Any advice is appreciated.

    jg2009 wrote:
    I was under the impression that classes without a defined package were technically considered to be part of the so-called default package. But, in answer to your question, yes this class does not appear to part of any package.
    Just to provide so additional information, the class that I want to instantiate is an Applet, so it is designed to be called from an html object tag, and have access to all of the other classes in its JAR. There is a system that provides access to underlying data via the following components: html form <-> javascript <-> applet <-> serverlets <-> data
    I want to automate certain data operations, but the only way to access the underlying data is via the applet. Am I out of luck, or is there some way that I can instantiate the applet?All jverd said is right, assuming it is your code and you can change it: basically move everything to a package. But I think your problem is that the jar is a 3rd party jar and you cannot change it. If this is the case, you can use the 'default package' (the terminology is correct) by having your own class(es) in the default package or by using reflection (last time I tried reflection would work). Both are rather bad solutions, but if you really, really want...
    Note: Java actively discourages the use of the 'default package' and more so recently, this is why I am not even sure that reflection would still work.

  • Importing a .class file into my package?

    Hi. I'm making a java program but I need to use an external .class file to get variables and use methods from. Is it possible to "import" a class file into my package to be able to do so?
    Thanks

    Basically, for our last project we created a class
    that held the information about a year: how many days
    were in a month, how many months were in a year, etc.
    etc. For this project, we'll be having to use that
    same class to make a GUI calendar. Our teacher
    provided a class which he compiled for us to use in
    our projects incase our previous class had some flaws
    in it, which mine did. Unfortunately, it's in .class
    format and I don't know how to use it in my package!That's going to be a problem then. I would assume that it is going to have an API like your old one did. Same name and same methods etc. So do you know what the name and method signatures of your class were supposed to be?

  • How to import classes in a package?

    I am importing all the classes in a package like:
    import packagename.*;
    when i am trying to create an object of the class in that package it was showing error...
    example.java:8: cannot access address
    bad class file: .\address.java
    file does not contain class address
    Please remove or make sure it appears in the correct subdirectory of the classpa
    th.
    address add = new address();
    ^
    1 error
    Below is my code....
    import java.util.*;
    import org.demo.*;
    public class example
         public static void main(String[] args)
              address add = new address();
              System.out.println("Address object created...");
    }

    class name address.class
    I am creating object as
    address add = new address()
    but it was showing the error

  • How can I import my class or package in JSP?

    Hello.
    I develop jsp. When I assign variables in jsp, everything work fine. For Example:
    connStr="jdbc:oracle:thin:@191.168.0.1:1521:orcl";
    But I want put some variables inside class or package, so using text editor I created and compiled package:
    package Pack1;
    public class Test1 extends java.lang.Object {
    public static void main(String[] args) {
    String ConnStr = "jdbc:oracle:thin:@191.168.0.1:1521:orcl";
    or class
    class Test1 {
    public static void main(String[] args) {
    String connStr = "jdbc:oracle:thin:@191.168.0.1:1521:orcl";
    But as soon as I put in my jsp line
    <%@ page import="Test1" %> or <%@ page import="Pack1.*" %>
    I always have an error.
    JSP Error:
    Request URI:/DateN/search/Search.jsp
    Exception:
    oracle.jsp.provider.JspCompileException:
    Errors compiling:e:\orant\oem8i\apache\apache\htdocs\daten\_pages\_daten\_search\_Search.java
    C:\orant\oem8i\apache\apache\htdocs\daten\_pages\_daten\_search\_Search.java:12: Package DateN not found in import.
    import Pack1.*; (or Test1)
    ^
    1 error
    I using OEM2.2 with Apache-Server under Win2000. I put my classes:
    Test1 in package: in folder: C:\orant\oem8i\Apache\Apache\htdocs\WEB-INF\classes\Pack1
    If just class, when just in folder: C:\orant\oem8i\Apache\Apache\htdocs\WEB-INF\classes
    If I using just class, when I put just in folder: C:\orant\oem8i\Apache\Apache\htdocs\WEB-INF\classes.
    Im stack. Anybody can help?
    Thank you.

    Another way. You can use a USB flash drive & the camera connection kit.
    Plug the USB flash drive into your computer & create a new folder titled DCIM. Then put your movie/photo files into the folder. The files must have a filename with exactly 8 characters long (no spaces) plus the file extension (i.e., my-movie.mov; DSCN0164.jpg).
    Now plug the flash drive into the iPad using the camera connection kit. Open the Photos app, the movie/photo files should appear & you can import. (You can not export using the camera connection kit.)
    Secrets of the iPad Camera Connection Kit
    http://howto.cnet.com/8301-11310_39-57401068-285/secrets-of-the-ipad-camera-conn ection-kit/
     Cheers, Tom

  • How to import a class that have no package

    I have .class archive that i need to use in a web aplication on Tomcat. I put the class in the directory: <webapp>/WEB-INF/classes/.
    The class doesn�t have a package, so when i tried to import (..page Import=".....") it, tomcat gave me an error because is looking for a package instead of the class.
    then i try to use it without the import and tomcat gave another error because it can�t find the class.
    Any ideas??
    by the way.. I don�t have the source code, I just have the .class archive..
    thank you....

    Also, I already found that the class had no package. Then you are out of luck.
    As of java 1.4 you can no longer import classes that are not in a package. [url http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=c85f07c1ce8f344d787b7a5146d68:WuuT?bug_id=4361575 ] Full details here
    The only solutions I can think of
    1 - get the people who originally gave you the class to package it properly.
    2 - Decompile it to java code, Change the java code to put it in a package and recompile it. Depending on the complexity of the class this may or may not be a trivial operation.
    Good luck,
    evnafets

Maybe you are looking for

  • Usb 6009 multiple analog inputs

    I am currently attempting to sample two different analog inputs at different sampling rates using a USB 6009.  I keep getting the 'resource reserved' error and am wondering if this is not possible using this DAQ.  Questions: 1.  Does creating two ana

  • Can't download apps to my ipod touch. It downloads to my screen and then it just says waiting...

    Pls help. I have two podcast downloads also. One saying processing and other waiting. I don't know how to stop and delete them.

  • Can No Longer Open PSE 7 or Premiere Elements 7

    I installed Photoshop Elements 7 and Premiere Elements 7 last summer and haven't had trouble with them, but as of a couple weeks ago I haven't been able to open either one up on my computer. When I tried to check the files in the computer, it said "E

  • Wireless Toolkit J2ME - changing the directory structure for your projects

    When I create a new package in the Sun Java Wireless Toolkit, the toolkit is creating a folder for the application and its subfolders in the following directory: C:Documents and settings/Owner/j2mewtk/2.5.2/apps/<name_of_project>.... I am trying to c

  • Problem with KOMV-KBETR field

    Hi ,     In VAO1, in field KOMV-KBETRi.e., label Amount when I  enter value' 60' and press 'ENTER' I get the value as '60,00'. Now, in my own  ALV Report output , I have a editable field ZNETPR of type KOMV-KBETR. Here, I enter value  '60' and press