Abt "import" statement & Inheritance in Java

Hi, All
As we (Java Programmers) know we'll be using "import java.Math.*" or similar for importing custom/Java packages.
If i have suppose imported "import java.math.BigDecimal;" in a Class 'A' & and iam extending this class 'A' in another class 'B', if i want to use any method from 'BigDecimal' here in this class(i.e., in class 'B') again i need to import "import java.Math.BigDecimal; " .
so, my question is :
1) When we import in one class a package and same package in another class does java import it twice (or) what it does?
2) when I extend a class , why don't this import facility don't get extended.(I think u got my point.)
PS: If any problems in this mail technical or what so ever, iam sorry.
Thanks in advance.

Importation is just a compiler trick. It has nothing to do with runtime.
The separation of sources files brings the necessity of repeated imports from file to file.
You can verify this if you create one source file and put several classes in it: you won't have to import
more than once.
so, my answers are :
1) Each time the compiler encounters an import package.Class statement, it checks the CLASSPATH for the class existence and loads the class definition.
When a reference to the class is done anywhere in the following statements, the compiler checks the referenced entity (member, method...) against the class definition.
2) If the extending class is contained in the same source file, then only one import will be needed.
Hope this helped,
Regards.

Similar Messages

  • How do i use an import statement in a java program

    I need to write a java program which will connect to the Database but i need to do an import like in DB2 which will load data to the table.
    How do i execute that import statement

    Are you saying that you need to add rows to a table in DB2 via a Java program? If so, you need to look at the JDBC API, (available on this site along with a bunch of tutorials). If not, post again with a clearer description of your problem.

  • Import statement in .java files and .jsp files ..correction

    Guys I have few mysteries about this import statement.
    Please correct me if I am wrong.
    1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement in two.java I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
    BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import one.class in My.jsp .That is i have to say
    <%@page import="one"%>
    in My.jsp.
    and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder and MyJsp is compiled sucessfully.(Note that My.class is created in a folder different from c:\ClassFolder )
    But I am wondering why was there no need for me to import one.class in two.java.
    (Note I am using JDeveloper and Apache server)
    Please help.

    It has to do with packages. Most java classes are in a package, the name of which must conform to its place on the filesystem relative to the classpath. By that I mean that if you have com.mystuff.One.java, it must be in a folder com/mystuff where com is located somewhere in the classpath.
    What you've done is a little different. I'm assuming a couple of things:
    1. you have no package declaration at the top of one.java or two.java
    2. you have the current directory "." in your classpath.
    Java has the concept of the "default package", which covers classes without a declared package, and in your case is the current directory.
    So when you're in c:\sourcefolder and run the compiler, then "."="c:\sourcefolder", and that directory is part of the default package. No import statements are necessary for classes that are in the same package. This is why two.java can call methods in one.java without an import statement.
    When you run your jsp, the "current directory" part of your classpath is not c:\sourcefolder, but some other value (probably the directory you start your jsp engine from) You will have to import all non-java-library classes because the jsp itself becomes a java class, with a package that is determined by the jsp engine.

  • Import statement in .java files and .jsp files

    Guys I have few mysteries about this import statement.
    Please correct me if I am wrong.
    1)Suppose if I have a folder c:\SourceFolder it has one.java and two.java files.If I compile one.java then one.class files is created in c:\ClassFolder .Now two.java uses one of the method of one.java,and without using import statement I am able to compile two.java, and two.class file is created in c:\ClassFolder.I assume that the path of one.class was taken from the CLASSPATH environment variable,hence I there was no need for import statement .........am i right ?
    BUT... in My.jsp,which is in C:\JspFolder, if I want to use one.class,then I have to specifically import the class I want to use.That is i have to say
    <%@page import="one"%>
    and since the classpath is C:\ClassFolder ,it finds one.class in the C:\ClassFolder.(Note that My.class is created in a folder different from c:\ClassFolder )
    But I am wondering why was there no need for me to import one.class in two.java.
    (Note I am using JDeveloper and Apache server)
    Please help.
    In this case the pSo I am wondering in JSP why it doesn't take the class I am looking for from the classpath .
    Help.

    You have to understand Java scoping rules. In the 1st scenario with the two Java files I am guessing neither one of them begins with a package statement. This means that they belong to the default package. When you compile the compiler uses the classpath to resolve method calls looking in the default package since there was no import statement specified in two.java. JSP is a little different. Now I just read this here in these forums a couple of days ago. Since the classfiles generated from the JSP files are not in the CLASSPATH they are loaded by a different classloader. This is why you have to import all references to all classes in the default package.

  • Java import statement problems

    Hello...
    I have the book "Java in a Nutshell, 2nd ed" and have a query about a nested top-level interface example they have in the book.
    Basically there's two files/classes. The first, LinkedList.java, is as follows:
    public class LinkedList {
        public static interface Linkable {
            // Linkable interface details here
        // rest of LinkedList class here
    Next is LinkedList.java as follows:
    import LinkedList.*;
    class LinkableInteger2 implements Linkable {
        // rest of LinkableInteger2 class here
    Now I thought the import statement had two forms - and both used a package name as part of the statement:
    import package.class;
    import package.*;
    Obviously, neither class has a package statement.
    As written, these classes do not compile. Am I right in assuming the lack of a package name is causing this problem, or is there something else going on here?
    Thanks...
    Mark.

    Since you don't have package statements in the files, the classes are being placed in the default package. You cannot explicitly import classes from the default package. Al classes in the default package, the java.lang package and the current package are always imported automatically. Your import statement is incorrect - remove it.

  • Java programming(import statement)

    My query is that what if i import whole package instead of one class being used in my code.
    eg.
    if i need " java.awt.Button" in my program code
    and my import statement is
    import java.awt.*;
    will it be reduce execution performance of my program?
    or it will reduce only Complie time performance?

    Only the compile time performance. All the import statement does is tells the compiler it can use the classes. Try importing a whole swathe of classes into a small program and see what I mean. The resulting class file doesn't bloat.

  • Confused about CLASSPATH and how java handles import statements...

    Hello,
    I must admit I don't get it. I read the articles about setting CLASSPATH etc. but I still wonder:
    If you use an import statement, what does the compiler do? I.e. where does it look for the specified classes? I find it confusing because I see in different locations different .jar files:
    C:\jdk1.3.1_03\lib\dt.jar
    C:\jdk1.3.1_03\lib\htmlconvertor.jar
    C:\jdk1.3.1_03\lib\tools.jar
    and also
    C:\jdk1.3.1_03\jre\lib\i18n.jar
    C:\jdk1.3.1_03\jre\lib\rt.jar
    C:\jdk1.3.1_03\jre\lib\jaws.jar
    C:\jdk1.3.1_03\jre\lib\sunrassign.jar
    Can someone explain me what the purpose is of these files?
    And why do I have the same contents in
    C:\Program Files\JavaSoft\JRE\1.3.1_03\lib
    and in
    C:\jdk1.3.1_03\jre\lib
    Why is that?
    Thanks for answering my questions!
    -mike

    Thanx for the answers, but I still wonder, everyone
    here says I need to set the classpath, but I don't.Probably because your classes are already in the class path. The compiler/jvm also look for classes by themselves not just in jar files, when just a directory is supplied in the class path. And a period (".") is a valid directory.
    Programs importing different classes compile with no
    problem. So what's up with that?
    Presumably you are referring to your own code - because they are in the class path.
    Second, I still don't understand why the runtime needs
    the .jar files. The runtime uses classes, like String, that have to come from somewhere.
    This would also mean that end-users
    need to set the classpath to the .jar files in their
    JRE directory to be able to run programs that import
    classes from these .jars. But this is not true, right?No it is true. The end-users will have to set the class path. There are variations on this which make it seem like no class path is set. For instance applets in a browser are java but the end-user does not need to set a class path. That is because the browser knows how to download classes/jars and how to set it up so it uses them. (Actually it uses a class loader, but that is probably more information that you need.)
    Because if I make some nice classes myself and import
    them, how can I expect my end-user to install these
    classes and make a classpath for them?That would be between you and you end-user.
    First installation is not part of java. For installation you will have to find something outside of java to accomplish the goal.
    Additionally how the class path gets set is OS specific. Java does not deal with that. You will also have to find some way to deal with this (most likely part of the installation.)
    There are also variations on this. For example the browser example I gave above. Or using the ext directory. Or creating an executable jar. Or simply setting the class path.
    In my understanding it should only be needed in the JDK, not
    in the JRE. True or am I mistaken?Mistaken. The class path is needed in the JRE as well. You will need to set it.

  • Java import statement memory problem

    Hi,
    will java.io.* loads all the classes in io package to the memory? will it afftect the program performance?

    RajBabu wrote:
    I hope that import statement might present in a java class fileNo, import statements are in no way represented in the java class files. All references to external classes are made via their fully quallified class names in a class file (com.foo.Bar and not just Bar*).
    Imports only serve as a shortcut so that you don't have to write the full class name everywhere in your Source code. You could just as well write a program without using any import statements, just by using the fully qualified class names of each class you use and it would produce the same class files (although the source code would be rather unreadable).
    * actually the format is different, but the idea is the same

  • Why the need of "import" statements for Java base code?

    If for documentation purposes, a compiler option to produce an "import list" would have been sufficient.

    import statements act as really simple namespaces.
    Instead of the programer having to specify:
    java.io.File f = new java.io.File("c:\\");
    the code can have a 'namespace' added to do the following:
    import java.io.file;
    File f = new File("c:\\);
    it's a way of getting around similiarly named classes, but still have the ability to drop the 'java.io.' part.

  • Executing db2 import statement from java

    we have developed an web application using J2EE technology on websphere and DB2 as a backend.
    I have to now develop a utility for data uploading from Excel to DB2. we can do it using IMPORT statement now i want to know is it possible to execute IMPORT statement from JSP/ Servelet and using javabeans.

    Yes, it is possible :-)

  • Jsp declaration and import statement

    Hi All
    I have some problems with JSP.
    I have wrtten a declaration in jsp like this.....
    <%@ page language="java"%>
    <%!
    Date date = new Date();
    List al new ArrayList();
    public List getList(){
    al.add("Hi");
    return al;
    %>
    The List is-> <%= getList()%>
    Note that i did not import anything, it gives the output without any compile time/runtime error. How it is happening?
    If i write any thing in scriplets.......like List, Date, it is giving compile time error asking to import this.
    I am using weblogic8.1
    May i get the correct answer for this ASAP?
    Thanks and Regards
    Kasim
    [b]

    weblogic includes certain import statements in the JSP page when it is compiled.
    That is why u r not getting an error some times!!
    Check your weblogic documentation to find more details abt that
    But it is better to include the import statements , so the code will work in any server.

  • Packages, import and Inheriting with static

    Hello.
    I am programming a program, in which the main class (file: Main.java) uses objects declared in Components.java.
    The problem is, the import of the components only works if I explicitly make the main class inherit from the components class with whose objects being declared as 'static'. A simple import instruction, the way I learned it, in the main class such as 'import <project>.components;' does not work.
    Sample code:
    Current state; working
    Main.java:
    package <project>;
    public class Main extends Components {  
        public static void main(String[] args) {
            start();
    }Components.java:
    package <project>
    public class Components {
        public static void start() {
            // code
    }Desired:
    Main.java
    package <project>;
    import <project>.components;
    public class Main {  
        public static void main(String[] args) {
            start();
    }Components.java:
    package <project>
    public class Components {
        public void start() {
            // code
    }How to realize this idea into a working program?
    Thanks in advance!

    I apologize for not conveying my problem proberly, but you have to know I did not learn Java with reference material in the English language, therefore it might happen that some terms are not translated correctly (by me) and thus not equaling the standardised terms.
    Anyway, let me try to rephrase my problem.
    I want to use the object declared in Components.java in the main class/main methode of the file Main.java. In order to achieve this goal I used inheriting, concretely the main class inherits from the components class, as a makeshift to use Components.java's objects in the main class. However this is not the way I want to accomplish my objective with.
    To make a long story short I just want to utilise Components.java's object in Main.java's main method. The manner to achieve this does not matter, though I want to do it preferably with 'import'.
    Hope this clears things up. Thank you anyway.
    Edited by: Ikaragua on May 31, 2008 6:00 AM
    Solution found.
    package project;
    public class Main {  
        public static void main(String[] args) {
            Components.start();
    }

  • Constructors and Inheritance in Java

    Hi there,
    I'm going over access modifiers in Java from this website and noticed that the following output is displayed if you run the snippet of code.
    Cookie Class
    import javax.swing.*;
    public class Cookie {
         public Cookie() {
              System.out.println("Cookie constructor");
         protected void foo() {
              System.out.println("foo");
    }ChocolateChip class
    public class ChocolateChip extends Cookie {
         public ChocolateChip() {
              System.out.println("ChocolateChip constructor");
         public static void main(String[] args) {
              ChocolateChip x = new ChocolateChip();
              x.foo();
    }Output:
    Cookie constructor
    ChocolateChip constructor
    fooI've been told that constructors are never inherited in Java, so why is "Cookie constructor" still in the output? I know that ChocolateChip extends Cookie, but the Cookie constructor isn't enacted when the new ChocolateChip object is defined... or is it?
    If you can shed any light on this, I would greatly appreciate it.
    Many thanks!

    896602 wrote:
    I've been told that constructors are never inherited in JavaThat is correct. If they were inherited, that would mean that, just by virtue of Cookie having a c'tor with some particular signature, ChocoChip would also "automatically" have a c'tor with that same signature. However, that is not the case.
    , so why is "Cookie constructor" still in the outputBecause invoking a constructor always invokes the parent class's c'tor before any of our own c'tor's body executes, unless the first statement is this(...), to invoke some other c'tor of ours. If this is the case, eventually down the line, some c'tor of ours will not have an explicit this(...) call. It will either have an explicit super(...) call, or no call at all, which ends up leading to the compiler generating a call to super().
    Note that the ability to call super(...) does not mean that that c'tor was inherited.
    I know that ChocolateChip extends Cookie, but the Cookie constructor isn't enacted when the new ChocolateChip object is defined... or is it?Yes, it is. As I pointed out above, if we don't explicitly call this(...) or super(...), then a call to super() is inserted by the compiler.

  • Import Statement Different ways to refer ???

    Dear Friends,
    Can any one help me the difference between the usage of import statement....
    1. using import statement
    2. using the class directly
    that is,
    for example ;
    1. import java.util.Date;
    Date dt = new Date();
    2. java.util.Date dt = new java.util.Date();
    what's the difference between two reference of Date class...is there any meaning beyond this ?????
    Regards,
    V.Prasanna

    what's the difference between two reference of Date
    class...is there any meaning beyond this ?????there is no difference once the code is compiled - the compiler resolves single class names to their fully-qualified names (ie with the package prefix) and that appears in the .class file constant pool
    before compilation then my opinion is that having all the imports at the top of the file is good practice because you can see what is being used easily

  • Import Statement Error

    Does anyone know what is wrong with these import statement?
    import Reduction;
    import Mapper;
    import ApplyObj;
    import ApplyObjUnary;
    import java.lang.String;
    public class Driver{
    public static Driver me = new Driver();
    public static void main(String[] args){
    int i;
    I places all my *.java file in the same directory as Driver.java
    But when i try to compile Driver.java, it says :-
    C:\assignment1>javac Driver.java
    Driver.java:10: '.' expected
    import Reduction;
    ^
    Driver.java:11: '.' expected
    import Mapper;
    ^
    Driver.java:12: '.' expected
    import ApplyObj;
    ^
    Driver.java:13: '.' expected
    import ApplyObjUnary;
    ^
    4 errors
    I set my class path as
    C:\>SET CLASSPATH = .;C:\j2sdk1.4.0_01;c:\assignment1
    Apparently, the current directory has been set and my assignment1 directory has been set.
    Did i miss out anything? Why does it can't recognize the existence of other class file in the same directory?
    thanks.

    You don't need to import classes that are in the default package (i.e. you didn't put them in a package). And in Java 1.4, you can't import them. Just remove the import statements, you shouldn't need them.

Maybe you are looking for

  • Help setting up my new IPod Touch on iTunes on windows 7 64bit

    Hello there, I need help setting up my new IPod Touch to my Itunes 10 application on a PC running windows 7 64bit. I have watched the tutorial on how to do it but what the tutorial says it is supposed to do is not what is happenning for me: 1. In the

  • Can't plot image into WDT waveform graph

    I'm trying to plot an icon onto a WDT waveform graph but it doesn't seem to be working. The image plots while using the default (array of doubles) data type, but goes missing when I change to WDT. As far as I can tell, the two graphs are the same exc

  • How to create reports in java or j2ee???

    I want to know how can we create reports in java or j2ee like we use crystal report in vb or .net.do we use jsp or servlet to generate the reports or is there any other tool to do the same?Members,please,do help me.

  • Need an Outlook PST repair tool for MS Outlook 2010

    My Outlook mailbox messages are corrupted. Someone has suggested to me use advanced PST repair tool. I have no idea about this one utility. Can anyone suggest useful reference ? Regards: christa

  • How to set alpha to TextField?

    I've created a txtField with background set to true, backgroundColor set to a certain color, and txtField.alpha set to 10; however, the alpha does not work. The background color of txtField appears to have a full opaque color and not transparency. An