java imports


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: java imports

  1. #1
    Join Date
    Sep 2004
    Posts
    4

    java imports

    i am having trouble importing a program into the java program that i am creating

    i want to import this file called learning

    so i enter

    import learning.*;

    the program is there and there is only one, i dont understand why it doesnt work though


    can anyone help?

  2. #2
    Join Date
    Sep 2004
    Posts
    150
    This may come off as a silly question. But are you instantiating an object that you got from importing that? If not you are not really using it.

    Forgive me if this is way off. There is no way to determine someone's skill level over the internet. And it might be better to answer than to not answer.

  3. #3
    Join Date
    Sep 2004
    Posts
    4
    No. its not way off and thanks for answering.

    The thing is im taking a java class in high school and an just a beginner.

    anyway... I think i am actually inheriting a method from this program by importing it.

    i hope that answers your question??

  4. #4
    Join Date
    Sep 2004
    Posts
    150
    i am having trouble importing a program into the java program that i am creating
    I think you're doing it correctly but you're missing one step. Using what you've imported.

    When you import something you essentially expand the java language. That is one way of thinking about it.

    Now you have these new abilities that this "learning" package gives you.

    I don't know the nature of the learning package but here is an example of how it could work.

    Lets assume that "learning" is a package that contains these classes (abilities):

    DateStuff - gives you methods for working on dates
    InterestStuff - gives you methods for calculating interest

    import learning.*; // gives you both of the above

    or

    import learning.DateStuff; // just gives you that one

    // now that you know you have these extra abilities you
    // can use them:

    DateStuff myDateStuff = new DateStuff();

    This is called instantiating (creating) an object. Here the "DateStuff" class that you imported is like a cookie cutter, and myDateStuff is the cookie that is cut out of the dough (memory). Now you can do whatever you want with it. This analogy isn't perfect but it works in the basic sense.

    // assuming DateStuff had a method called doThings()
    // and doWhatever()

    myDateStuff.doThings();
    myDateStuff.doWhatever();


    If you were making a program that needed to be able to tell someone the day of the week that a date fell on and you were just missing that one ability you would do something like this:


    // your program here
    public static void main(String args[])
    {

    // to keep it simple we'll ignore the interaction with the
    // user which is a topic all by itself
    // say you just needed to know the day of the
    // week that October 24th, 2016 is and knowing that
    // would just make your program great. Let's assume
    // the "learning" package
    // you are importing has this amazingly useful ability..

    DateStuff myDateStuff = new DateStuff();

    // assume that DateStuff has this ability below

    String day = myDateStuff.getDayOfWeek("October 24th, 2016");

    System.out.println("October 24th, 2016 is going to be a " + day);

    // here you have used this extra ability inside
    // your own program and didn't even have to know
    // how it worked.

    // the rest of your program goes here..blah blah blah
    // stuff you made up..etc

    }


    Now I have no idea what abilities importing "learning" gives you this is just a random example. It could be that learning has many classes inside of it that you need to make use of. In that case you would repeat as necessary for the other classes. The learning.* part gives you all of them by default.

    Here is the same program without the comments:

    public static void main(String args[])
    {

    DateStuff myDateStuff = new DateStuff();

    String day = myDateStuff.getDayOfWeek("October 24th, 2016");

    System.out.println("October 24th, 2016 is going to be a " + day);
    }

  5. #5
    Join Date
    Feb 2004
    Posts
    541
    I don't think you actually understand how to import a class, or what a package is. Java locates things using the classpath and packages. The classpath identifies key locations on the computer where java classes are located, and the packages then locate the actual classes.

    Code:
    import learning.*
    The above code does not (necessarily) import a class named learning. What it does do is attempt to find a package on your computer called learning, and then import all of the classes inside this package.

    What package is your learning class in?

  6. #6
    Join Date
    Sep 2004
    Posts
    4
    I think my problem is that i dont know how to set the classpath so that my computer can actually find this package.


    i think that it is located in jdk1.2.1\jre\lib\learning

    How would i set the class path though?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center