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.

Similar Messages

  • 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);

  • Why do we import a class

    Why do we import the class TestFile and not call the class instead like in the code given below
    package test;
    import testing.TestFile;
    import java.util.StringTokenizer;

    "import" basically just defines aliases for class names. It lets you specify just the class name, without the full path. It makes the code easier to read.
    You don't really call classes at all in Java. You call methods. You can do that later in the code.

  • Import a class cannot take effect until the restart of wls6.1,why?

    Hi all,
              I am using Weblogic6.1,I knew that JSP,Servlet,EJB can be auto depolyed
              without restart weblogic,but when I
              import a class written by myself in JSP or Servlet£¬the modification of this
              class cannot take effect until
              the restart of weblogic server¡£Why? How can I solve it£¿
              thanks in advance!
              

    Check that your classpath is not set up to this class when you start the
              server and check the ProductionDeploymentMode to false in the starting
              parameters!
              Hope it helps.
              Pat
              "jxc" <[email protected]> wrote in message
              news:3bef34c3$[email protected]..
              > Hi all,
              > I am using Weblogic6.1,I knew that JSP,Servlet,EJB can be auto
              depolyed
              > without restart weblogic,but when I
              > import a class written by myself in JSP or Servlet£¬the modification of
              this
              > class cannot take effect until
              > the restart of weblogic server¡£Why? How can I solve it£¿
              >
              > thanks in advance!
              >
              >
              >
              >
              >
              

  • How to modify a specific class in jar file ?

    I've downloaded a jar file for applet, the jar file works fine... but when I extract a specific class file from the jar file and just recompie it from my IDE (Eclipse) without even making any change and then compress again jar class files including the new modified class file instead of the old one (without any modifications to other classes)... then I get
    (NoSuchMethodError ) exception whenever methods of other classes are invoked from within the modified class !!
    ...The manifist file of the jar file reads the following line
    Created-By: 1.4.0_01 (Sun Microsystems Inc.)
    I thought it means that jar class files were built using JDK 1.4.0_01 ...I used JDK 1.5.0_01 in my IDE...
    I thought JRE incompatiblity was the reason of the problem so I downloaded JRE 1.4.0_01 and used it to build this class file... but i got the same exception..
    so what is the main reason of the problem ? ...should I make changes to IDX files accompanying applet jar files ??
    If no, then how can I overcome this problem and be able to change and rebuild a specific class from this jar file ?
    (I cannot rebuild all classes in jar because there are errors I cannot resolve !!)

    Could you please clarify: do you want to run your project or a project from an independent jar?
    In the first case just select Run Project from the project context menu or select a class with main method and click Run File in the class context menu.
    Regarding the second case:
    - I don't think there is such a feature in the IDE (running third party jars is not an IDE function). Could you explain why you need this?

  • How to run the imported java class in form

    Help!!!!!
    Pls help me to run the imported java class in forms.
    Package is created in forms while imported one class called
    singlexml.class and that package has one procedure and one
    function.
    I just wanted to run that class.I mean the new package.
    Thanks
    Anil

    Hi,
    It is because the converter works on byte code and it only supports a subset of the Java language (see the JC specifications). It is kind of like compiling you code on Java 6 and trying to run it on Java 5. The JCDK outlines the required compiler version.
    Cheers,
    Shane

  • Problems importing a class with inner class

    Why can't I import a class so that I can use an (inner) static member class without naming the enclosing class. Put simply, I have:
    file C:\MyJava\Test\A:
    public class A {
      public static class B { }
    }file C:\MyJava\Test\C
    import A.*;
    public class C { }When I try to compile C (javac -classpath C:\MyJava\Test;. C.java) it fails with C.java:1: package A does not exist

    Class A must be in a package if you wish to import it. If you want to use the inner class, just import somepackage.A. You could then refer to A.B...
    HTH :o)

  • Classpath problems when importing my classes

    I'm using a tutorial with sample programs dealing with bank accounts.
    I get this error:
    InterestBearingAccount.java [9:1] '.' expected
    import Account;
    ^
    1 error
    Errors compiling InterestBearingAccount.
    The Account class already exists in the current directory. I've tried everything as far as setting the classpath variable (nothing, just a period, period & slash, the actual complete path for the current directory, nothing works!!!!!). I also tried using -classpath (.) and the end of my javac command and that seemed to not work at all.
    The Java Coffee Break tutorial have some already compiled (*.class) files and they run fine. My *.java files are identical. In fact, I cut and pasted them so I know it's not a typo.
    My path is set to include the jdk1.4.0\bin directory and I've only begun to have problems now that I need to import existing classes into the programs.
    I should mention that I get the same error using Forte environment or doing it at the command prompt window. Please help.

    Thanks ATMGUY. I commented out the import statements in both InterestBearingAccount and AccountDemo and it worked fine. I guess the author of that tutorial was obviously not working with jdk 1.4.
    As far as using packages, com.kusek.bank would make a good package name then??
    The statement:
    package com.kusek.bank; OR a shorter version package kusek.bank;
    should be added to all three classes and no import statements would then be necessary, right??
    I just remember reading that it should be something unique as a good habit to get into.
    I'm trying to learn enough to make a useful GUI program for a special project so I probably should be using packages anyway when I get to that point.
    My programming backround is limited to procredural languages Basic, QBasic, Pascal, and Fortran and I find myself still wanting to think in those terms. I have read several web sites and worked through some basic examples but I still have much to learn. I once I can grasp how to write OOP style it would be nice to look at code for a somewhat detailed application (like a store checkout app) and have every line explained as to why it's there. I can see where it makes sense to have your GUI classes predefined (Swing), File I/O, serial port communications, and even things like this InterestBearingAccount example that extends from the Account class utilizing its basic attributes but overriding the constructors for the additional parameters (interest calculation) that are passed. How to take a common problem and go about creating a solution is what I need to see being done to help me.
    My telemetry project uses VHF radios with packet radio modems to monitor two diesel engines and turn one off if the other turns off. The modems have built in A/D data gathering and can also output control signals, they just need to be told what to do. I can manually use a Windows Hyperterminal session and type in "connect Engine1" and use commands to determine the A/D inputs "analog". The modems have built in end-to-end acknowledgement so the only think I need is an application than can be setup to run in the Startup folder to handle this. It would use a timer class to "poll" engine1 every 5-10 minutes and if it has stopped (based on the response of the "analog" command) I would "disconnect" from engine1 and "connect" to engine2 and send a "ctrl B off" or something similar. The standard responses from the modem should allow me just to send and recieve STRING data to and from the serial port and parse them as needed. I need at least this much but it shouldn't be too much harder to develop a way to reset the system(reverse of this) when that is required. I could also read a water pressure value at one of the locations and display that on the screen along with the current status of the engines (using values obtained from the "analog" command on the modems).
    The hardest part of OOP programming seems to be knowing what classes are already out there and knowing how to find them or apply them. I hope Swing has classes that provide for displaying values on the Windows taskbar (my motherboard monitor freeware displays temperature readings down on the taskbar). I appreciate all the help given here. It's almost like having a private tutor.
    randy

  • Importing packages, why import more than once

    I've seen the following in a textbook:
    import java.awt.*;
    import java.awt.event.*;
    // the rest of the code follows Isn't this redundant? When we use the asterisk after java.awt, doesn't that import all classes inside the awt package, including the event package? Is there something I'm missing / not understanding.
    thanks in advance for your help.

    Thanks Vanilla, your post helps. Can you expound any
    more? I would have thought the relationship was that
    "awt" is a package that contains both classes and
    other packages, and that "event" is a package in the
    "awt" package. It would seem that way, but it's not.
    Why does Java use the dot notation if the packages
    aren't hierarchical? I guess because, even though the packages aren't related as far as Java is concerned, there's a conceptual relationship that we humans use, and the dot notation matches that. Plus the elements of package names correspond to directories, so while the language doesn't care, ClassLoaders do use that aspect of the name to find the class.

  • Why use Factory classes

    Why do we use Factory classes such BorderFactory, to get an instance of Border class, why cant we directly create an object of such classes like we do with regular concrete classes

    Why do we use Factory classes such BorderFactory, to
    get an instance of Border class, why cant we directly
    create an object of such classes like we do with
    regular concrete classesFactory methods (I mean static create methods here) have two advantages.
    First, the library designer doesn't have to expose concrete classes to the outside world, just interfaces. This adds flexibility.
    Second, the library designer is free to make object creations more flexible. The create method may take parameters that doesn't exactly correspond to a constructor of one specific class.

  • Import local class to global

    Hi,
    i try to import local classes to global classes and i have error ,
    in the program in se 80 (lcocal) i declare global declaration
    like:
    TYPE-POOLS icon.
    TYPES: ty_fuel TYPE p DECIMALS 2,ty_cargo TYPE p DECIMALS 2.
    and after that i declare the local class in includes .. and/// start of selection
    where i put it ?
    Regards

    Hi,
    i don't build another program the declaration is in the local progarm,
    and i have method that have to use icon ,
    where i put this declaration  i try in local types but i have error .
    Explicit length specifications are necessary with types C, P, X, N, and
    W in the OO context.
    Regards

  • What is actually happenning when we are importing a class of some other pac

    Sirs,
    I have a doubt which is very basic to java,
    I want to import a class say "HashMap" to my class.
    If i put import "java.util.HashMap" or "import java.util.*;"
    Which will be better ?
    If i put "import java.util.*; " will it affect the performance?
    What is actually happenning when we are importing a class of some other package to our class
    Your help solicited,
    Sudheesh K S

    Many of your questions of this nature can be quickly answered by a search of prior posts to the forums, like this:
    http://forum.java.sun.com/thread.jspa?forumID=54&threadID=631979
    And why do you tend to post boldened text? It's harder to read.

  • Importing unpackaged classes

    Hi,
    Here is a simplified example of what I'm trying to do. From a packaged class, I want to instantiate an object which is of type of an unpackaged class.
    package foo.bar
    public class ClassA {
    public ClassA() {
    public static void main()
    ClassB b = new ClassB();
    public class ClassB {
    public ClassB() {
    When trying to compile .. (where ClassB.java is in the directory C:\ClassB)
    C:\foo\bar> javac -classpath C:\ClassB ClassA.java
    I get the following compilation error:
    ClassA.java:5: cannot resolve symbol
    symbol : class ClassB
    location: class ClassA
    ClassB b = new ClassB();
    ^
    ClassA.java:5: cannot resolve symbol
    symbol : class ClassB
    location: class ClassA
    ClassB b = new ClassB();
    ^
    2 errors
    I see no reason why the above example should fail, or am I doing something incredibly stupid? Anyone have an ideas?
    Cheers
    Andy

    If you are using J2SDK1.4, you can not use a class from an unnamed package in a class in a named package. In 1.3, you can import the class and it will work (assuming it is in the Classpath). Your only choice is to package the class or use 1.3 - I'm not sure what earlier versions support.

  • Importing Java Classes for Bussiness Logic in Forms6i

    I have created a Java Class with some bussiness logic in it and i m able to impotr it in Forms 6i BUT when it converts my java method into program unit it automatically changes the name of the method with some code ..like method name is "increment" it will make it as "increment_3137" and also one parameter of Ora_java.object type will be added ....WHY is THAT ???and how to work for hat ...even when i m executing the method with the parameter it gives some Exception that Non-Oracle Exception ...and my class individually runs perfectly fine ...CAN U HELP ME ASAP ...Please as its urgent
    Thanx..

    Once you have imported java class files on forms throgh java import class option..
    it creates a pl/sql for the java class that you have imported.
    names for the procedures are assigned by machine code, so dont worry about it..
    for eg. if the name of a method in java class is Increment and after loading java class file the name of ur pl/sql is increment_1254.
    when u call increment_1254 and pass parameter to the procedure ur class incerement will be called and desired operation will be performed.
    ora_java.object is the objec that will be used to access the the class file.
    as java is object oriented it requires a object to execute method of class.
    so u have to initialize the object..
    e.g. increment_1234 (ora_java.object,a,b) is ur procedure
    to call this procedure do the following
    declare
    PASS ORA_JAVA.JOBJECT;
    a,b integer;
    begin
    pass := <classname>.new;     --new will be created if ur class is public or u got public constructor in ur class.
    increment_1234 (pass,a,b) ; -- increment_1234 (ora_java.object,a,b) ;
    end;

  • Import a class directly in classpath,without package.

    In jdk1.3, I can
    --------------------------- import MyClass;
    MyClass is in my classpath.
    But in jdk1.4,
    --------------------------- import MyClass;
    cannot been compiled success.
    exception: "." expected.
    Why?

    That's right. Importing a class that wasn't in a
    package was always pointless, and in 1.4 it's actually
    illegal.I am not as certain that I believe in the 'pointless' part.
    As it stands now, a class that does not have a package can not be used in a class that has a package. Not that that is a great loss, but the result certainly surprised me.

Maybe you are looking for

  • How to create custom infotype for training and event management

    hai freinds can any one tell me how to create custom infotype for training and event managment with following fields PS No – PA0000-> PERNR Name   - PA0001 -> ENAME IS PS.No. – PA0001-> PS no. of Immediate Superior IS name PA0001 -> ENAME thanx in ad

  • Database logon failed

    Hi everyone, I know this is a famous error in CR but due to my special case, I need to make sure before enacting any further actions. We are developing a java application which uses CR2013 as its report server (32bit) installed on a 2012 windows serv

  • How to handle pagenation for tree table

    Hi, I implemented af:treeTable but, that table is displaying 20K rows(childerns). When i ckick on the views->Expand All, taking 30min to load the treeTable. Is there any wan to handle this problem or is there any way to pagenate the treeTable? Regard

  • Screen painter info req?

    Hi Can any one tell me how to make a select option in the screen for a filed in the module pool. Regds.

  • When to use collection?

    Sorry to post such question in this forum , but i would like to recieve some interesting about this . I know the basics of collection but i dnt know when to use which one? I am totally confused that all are working for the same thing with little diff