The 'import' statement

Dear Friends and fellow Javatizens....
I have a package called 'Tester'. In this package i have two classes caled 'Fruit' and 'Food'.
In the 'Fruit' class I have an integer varibale 'i' which is assigned the value of 5.
Now create another class called 'Food' and import the 'Frult' class as 'import Tester.Fruit'. I come up with a println statement in the 'Food' class that says:
System.out.println("The value of i is " + i);
I am getting an error saying that variable 'i' can't be found in the class 'Food'. Why is this the case when I have already imported the class 'Fruit' which already has the integer 'i' declared as a public int?
It will be very useful for me if somebody were to shed light on this matter. Thank you very much.
regards
Compiler

when you type import you say to the compiler that you dont want to put the full class name every time you use that class.
System.out.println("The value of i is " + i);That code prints the value of the variable i, but the variable i is not declared.
The class Fruit has that variable, but if you want to use you should type
Fruit f = new Fruit();
System.out.println("The value of i is " + f.i);or
System.out.println("The value of i is " + Fruit.i); // if i is staticI think thats it!

Similar Messages

  • How to organize the import statements in FXML Files?

    Hello,
    I'm a user of NetBeans IDE and it can organize the import statements in *.java files. Now, I'm using JavaFX and editing the *.fxml files as well. I want to know how to organize the import statements in *.fxml files?
    Thanks a lot.

    You should use Scene Builder, it will make all these import statements for you.
    Moreover, it will greatly simplify the writing of your FXML files, since you only have to Drag&Drop JavaFX objects on the scene. Then, it automatically generates FXML files.

  • Please explain the IMPORT statement?

    I have been searching for information on the import statement, but can't seem to locate it. I understand how to use import, but I would like to know, in detail, exactly what it does.
    When I import, for example; "java.awt.*", I would expect this statement to import ALL of the classes define under "java.awt". However, I notice in the DiveLog class, that I must also import "java.awt.event.*". This is confusing to me.
    Can someone either explain this for me, or point me to the imort documentation?

    aye - what yawmark says. And also be aware that import doesn't really "import" any source (not the way #include does, if'n you're an old school C coder). It's just there to shorten typing. You can either type "java.io.IOException" a lot, or import java.io.IOException and get away with just typing "IOException". But you've just "imported" the namespace, not any kind of source.
    Make sense?
    Lee

  • About the import statement

    Hi, im trying to use NumberFormat to format a double variable.
    How do i know if i need to use the "import java.someclass" statement or not? thanks for reading.

    NumberFormat is in the java.text package, so you either have to import it via:
    import java.text.NumberFormat;
    or
    import java.text.*;
    or else everywhere you use it fully qualify it as java.text.NumberFormat

  • Error showing in the import statement

    Hi there........
    I am working on mobile project of expense managemnt.
    in that code i started with following statements.
    import java.sql.*;
    i am using the CDC 1.0 toolkit.
    it shows the error that statement does not exist & errors for class.
    Is it necessary to use additional API for implementing embedded SQL in mobile.
    SO,
    please tell me immediately

    hi thanks for your reply
    i have loaded tomcat4.1 exe -- for windows i am starting the tomcat server by clicking from the start menu..
    i dint set any env variable .. tell me how to set...
    regards
    ijay

  • Java Newbie Question - the import statement

    Hi Geeks,
    I have a problem for importing a java .class in my project. This latter is named "Tedetis_New". Inside it, I created a "src" folder containing all the source code of the application. I import a jar file inside the parent directory of "src" (i.e. at the root of the project). Inside this jar archive, two classes (.class files) are not placed inside a package (default package according to eclipse), let's name one toto.class. So when I want to import toto.class from a file inside the src directory I simply do "import toto.class" but this statement doesn't work ! I don't manage to import my toto.class so ... what do you propose for this ?
    Thanks.

    Don't use the "default" class for anything serious;
    you can't import such a class.Er, package maybe?Yes, I was editing my reply while you replied to my reply so I couldn't
    edit my little blooper in my reply anymore; thank you very much Sir ;-)
    kind regards,
    JosI entered that response as quickly as I could, for just that reason. I
    thought you might notice and try to correct it, and I wanted to preserve
    your fuckupus maximus for all eternity.
    Everyone gather round and taunt Jos! Wave your private parts at his
    auntie! Fart in his general direction!
    Now, aren't you glad you didn't say "Jehovah"?I already knew that you were the one who invented amiability ;-)
    kind regards,
    Jehov^H^H^H^Hos

  • Length error occurred during the IMPORT statement.

    i have problem in Zprogram.its working fine in 4.6b ,but its problem in ECC5.0.its giving dump and saying
    Error analysis                                                                     
       An exception occurred. This exception will be dealt with in more detail        
    below. The exception, assigned to the class 'CX_SY_IMPORT_MISMATCH_ERROR', was 
    not caught, which  led to a runtime error. The reason for this exception is:         
    The system found when importing that the target object was longer or   
    shorter than the object to be imported.

    Hi Madhu,
    Suggested to post this in logistics forum for better answer.
    Software Logistics
    Regards,
    Debasis.

  • Error on the import statement

    // chekdb1.cpp : main project file.
    #include
    "stdafx.h"
    #include
    <windows.h>
    #include
    <stdio.h>
    #include
    <conio.h>
    #include
    <iostream>
    using
    namespacestd;
    #import
    <"C:\Program Files (x86)\Common Files\System\ado\msado15.dll">rename("EOF",
    "EndOfFile")
    int
    main(intargc,
    char* argv[])
    HRESULT hr = S_OK;
    try
             CoInitialize(NULL);
    // Define string variables.
            _bstr_t strCnn(
    "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;User ID=ma;Initial Catalog=UNI;Data Source=FALCON;Workstation
    ID=FALCON");
           _RecordsetPtr pRstAuthors = NULL;
    // Call Create instance to instantiate the Record set
          hr = pRstAuthors.CreateInstance(
    __uuidof(Recordset));
    if(FAILED(hr))
                cout<<
    "Failed creating record set instance\n";
    return0;
    //Open the Record set for getting records from Author table
          pRstAuthors->Open(
    "SELECT * FROM student",strCnn,
    adOpenStatic,     adLockReadOnly,adCmdText);
    //Declare a variable of type _bstr_t
         _bstr_t valField1;
    intvalField2;
         pRstAuthors->MoveFirst();
    //Loop through the Record set
    if(!pRstAuthors->EndOfFile)
    while(!pRstAuthors->EndOfFile)
              valField1 = pRstAuthors->Fields->GetItem(
    "id")->Value;
              valField2 = pRstAuthors->Fields->GetItem(
    "name")->Value.intVal;
              cout<<
    "%d - %s\n"<<valField2<<(LPCSTR)valField1;
              pRstAuthors->MoveNext();
    catch(_com_error & ce)
          cout<<
    "Error:%s\n"<<ce.Description;
      CoUninitialize();
    return0;

    You may get better help in C++ forum here. What is the error you're getting and which line of code generates it?
    For every expert, there is an equal and opposite expert. - Becker's Law
    My blog
    My TechNet articles

  • The Import statement could not be resolved

    We have are modifying standard MSS application through NWDI .
    But when we create project we are getting error
    Error: Could not load
    import com.sap.xss.hr.eeprofile.archiveddocuments.vac.wdp.IPrivateArchivedDocumentsView;
    MSS component is of SP 8 while portal components are on SP11. We are using Developer Studio with SP 11.
    Please suggest.
    Regards,
    Sudhir

    Hi Sudhir,
                     have u check that all the required jars are available for the project??
    regards
    Sumit

  • Including class files withouth the import statement

    I have been working on a project for my computer science class in which i have to design a "Projectile" class. However, when i attempt to use that class in a program, my compiler can't find the file even if it is in the same directory. I think i may have a problem with my classpath, but i am not sure. I don't know much about the way the compiler works. If anyone could give me a hint that would be great. Thanks

    You'll probably need to post more information, such as the command you are entering, the exact error message, and perhaps part of the code - at least the class definition.
    It sounds like a Classpath problem, or perhaps you aren't specifying the class correctly. The classes must be in the same package or you must import the_package.Projectile into the other class. The compiler will attempt to find the Projectile class by looking into the directories that are in the Classpath. You could try "javac -classpath . the_class.java"

  • Help with the import statement

    Hello,
    i have a question, how would you start a simple program
    that it doesn't involve graphics or anything like that.
    A simple program that for ex. it would increment and then dicrement the
    enetered number.
    What would you write after import_______________;
    and how would you initialize the variables that you use.
    Thanks in advance!!!

    I'm not sure that you understand what an import statment does.
    Every class that you want to use must be imported. For example in this the class Integer must be imported
    Integer myinteger = new Integer(5);so every time you use the word "new" that class must be imported.
    To make our lives a little easier java will automatically include the java.lang.* files so all java programs have the line:
    import java.lang.*;even though you don't see it.
    If you want to use other classes you must find out what package they are in and import that class or it's entire package.
    For example if you want to get user input you would probably want to look at the java.io package.
    It will take some time to get a good feel for the packages and what classes are available, but spend some time each day or a couple time a week looking through the java API's.
    hope that helps.

  • Filename in import statement

    Hi srinivas bobbala,
    Thank you for your response.But I think my quesition was not clear.My ques... is for suppose there is one datafile for importing like.. "datafile_21" Here my intention is it takes file from datafile_21 only but it appears in import statement like datafile_21<<curmon>>.
    import database sample.sample data from data_file "c:\\ABC\datafile_21_AUG.txt" using server rules_file datafile on error abort;
    In this it takes datafile from datafile_21.But it appears like datafile_21_AUG in import statement.This AUG coming from batch file.
    Essmsh c:\\ABC\loadmxl.mxl %curmon%

    No it is not possible.
    I assume the data file "datafile_21" is first renamed to datafile_21_${CurrMth} in the batchscript.
    After that this data file *datafile_21_${CurrMth}* is pointed in the import statement.
    In the logs you will see this file as datafile_21_Aug.

  • 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.

  • Import statement is not working

    hi everybody,
    I have a directory under which I have a number of sub-directories. Lets call this directory "parent_directory". under this directory I have another directory called "child_1". Under "child_1" I have another subdirectory called "child_2". Now I have a code in child_2 that has the following statement:
    import program_1;
    //this is a program present in the parent directory. lets call it
    //program_1.java
    when I am compiling I am getting an error saying:
    program_2.java:28: '.' expected
    import program_1;
    ^
    1 error
    program_2.java is the program present in child_2 in which I have included the import statement.
    If I change the ";" to "." then again error appears as the import statement only needs the name of the class and doesnot need the extension.
    I will be very grateful to you if you could please suggest me a way around this.

    This questions belongs in the "New to Java" forum ...
    sigh
    Anyways, import works on package names with trailing * or full qualified class names, e.g.import mypackage1.*; // imports all classes in mypackage1
    import mypackage2.MyClass2; // imports MyClass2 from mypackage2So instead of import program_1; you need to do
    1. ensure that program_1 is in some package
    2. ensure that your directory structure mirrors your package structure (same names!)
    3. ensure that your CLASSPATH is either not set at all or points to the parent dir of your topmost package
    Example:
    Directory structure:C:\tmp\java\top\
    C:\tmp\java\top\MyClass1.java
    C:\tmp\java\top\bottom\
    C:\tmp\java\top\bottom\MyClass2.java
    CLASSPATH=".;<java install dir>\jre\lib\rt.jar;C:\tmp\java"Source for MyClass1.java:package top;
    public class MyClass1 {
    }Source for MyClass2.java:package top.bottom;
    import top.MyClass1;
    public class MyClass2 {
      private MyClass1 myClass1;
    }

  • Import statement -- Filesize^

    How does the import statement work anyway?
    Will it just "paste" the imported files in front of th class like a C preprocessor?
    If so, is there a way to import packages only once?
    I use (e. g.) java.awt.point in nearly ALL classes in my project, and at the moment, I import the class in every single file. If it will be pasted, this will significantly enlarge my project, I fear. Or does the compiler recognize double imports?

    How does the import statement work anyway?The import statement just gives the compiler some info so you don't have to fully qualify the class names.
    Will it just "paste" the imported files in front of th
    class like a C preprocessor?No
    It doesn't put anything into the compiled class, so won't affect the file size.

Maybe you are looking for

  • Connect as...

    Ok here is my setup: Main computer: iMac 3.06, 2GB, 500GB, 24" Networked SETI box: Mac Mini 1.25, 256MB, 40GB 1.4.11 Mini has no mouse/keyboard/monitor I am no longer able to use connect as(through finder.). But I am able to use share screen, and rem

  • XML Parser Xtra

    Hi, I've used the XML parser Xtra in my movie. How can I distrubute it as a projector. Is the XML parser Xtra included in Director? how can I include it in a projector? Thanks 3ddev

  • PXI-6115 & PXI-6143 on Same Computer, RDA's Future

    Today I installed several PXI-6115 cards and realized that they are only supported with traditional DAQ under version 7.2. This is OK for now because I'm already limited to traditional DAQ to be able to use RDA. However, I was considering adding the

  • Queued State Machine, not a state machine?

    I've been reading up a bit on design patterns that seem popular among LabVIEW users, and I have a question with regards to the design pattern described in the following article: http://expressionflow.com/2007/10/01/labview-queued-state-machine-archit

  • Sony Camcorder and an ibook G4 - a love/hate relationship?

    Hi, I'm new to using a Mac and i'm keen to edit our summer videos, shot using a Sony Handycam DCR-HC18E. I don't think it's compatible -so what do i have to do? Is it worth keeping the Handycam or do i launch it up the Garden? Hope you can help