• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

Import classes

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi everyone,

I'm studying Java since two weeks. I already know the basements of C language. For Java I am using Eclipse IDE.

I have two questions for you:

1)How the process of importing classes works?  Of course, before declaring the class I just have to write "import ecc.". But how do the process itself works? What does the IDE do after that line of code?

2)How to import classes in my package?

The classes that I want to import in my package have to be simply online? Or do they have to be on my computer? Should I download them as a zip-file or something like that?

Thanks everybody.
 
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Claud Mann wrote:
2)How to import classes in my package?

The classes that I want to import in my package have to be simply online? Or do they have to be on my computer? Should I download them as a zip-file or something like that?


Ultimately, an external "package" (actually a jar file) needs to be on your computer.  (A jar file is basically a zip file under that covers.)  You can get these jar files on your computer one of two ways: You can use a build application, like Maven or Gradle, or you can download the jar files and set them in your project manually.  For Eclipse you right-click on the project in the project view and select Build Path -> Add External Archives.  Eclipse will then ask you where the jar file is on you computer.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, and welcome to the Ranch!
 
Saloon Keeper
Posts: 12811
278
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to CodeRanch Claud!

Claud Mann wrote:I already know the basements of C language.


Forget everything you know. Drawing on your C knowledge is more likely to hurt you than help you.

How the process of importing classes works?  Of course, before declaring the class I just have to write "import ecc.". But how do the process itself works? What does the IDE do after that line of code?


The IDE does nothing. It just feeds the code to the Java compiler. And all that the Java compiler does with import statements is add locations to search for a class definition when you use an unqualified class name in your code.

The classes that I want to import in my package have to be simply online? Or do they have to be on my computer? Should I download them as a zip-file or something like that?


The classes need to be "on the class path". What that means exactly depends on the class loaders that your compiler and JVM use.

In most cases it means that you need either a directory or a JAR file on your local file system that contains the class files that you want to use. You need to tell the compiler/JVM about the locations of all these directories and JAR files. You can do this with the -cp command line switch.

Let's say that your application uses two libraries: one that another company developed and released as a JAR file, and one library that you wrote yourself and is located on your computer in some directory. Imagine the directory structure looks something like this:

Imagine MyApplication.java looks like this:

To compile my-application, you would run the following command from inside the my-application folder:

The --source-path switch was used to tell the compiler where to find your source files. The -d switch was used to tell the compiler where to output your compiled class files. The -cp switch was used to tell the compiler where to find the third-party library, and where to find the compiled class files of your own library. Of course, it's also possible (and often preferable) to package your own library as a JAR and put the JAR on the class path, instead of the folder containing the class files.

To run the application, you would run the following command from inside the my-application folder:

As you can see, you also use the -cp switch to tell the JVM where to find all the compiled classes. This time we added our own bin folder to it, in addition to the third party library and your own library.

Now, all of this is pretty tiresome work, so there are tools that download third-party libraries and put all dependencies on the class path automatically. One such tool is Maven. When you use Maven in your project, this is what you do to compile your application:
This is what you do to run your application:
 
Claud Mann
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:Welcome to CodeRanch Claud!



Thanks everybody, expecially to Stephan! You've been very clear and accurate.
I realized I have a lot to study

Do you know which are the most famous websites where I can download these Jar Files containing written classes?

As an example, I would like to download classes containing methods regarding Statistics (Probabilities, Cumulative Distribution Function, etc.) and import them on my code.
I don't know where to search them.
 
Bartender
Posts: 2845
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Knowing C programming definately helps. I was a C programmer almost a decade ago !! It would be a different experience to work with object oriented programming. There's a learning curve to everything but loops and conditions and switch statements would look familiar. I also found that using references in Java was a very intuitive especially knowing how pointers work.
 
salvin francis
Bartender
Posts: 2845
150
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Claud Mann wrote:...As an example, I would like to download classes containing methods regarding Statistics (Probabilities, Cumulative Distribution Function, etc.) and import them on my code.


The word you are looking for is Jars not classes.
You can look at https://mvnrepository.com/ for open sourced libraries. Probably Apache math or Combinatorics is what you are looking for. Not sure though.
 
Claud Mann
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:

Claud Mann wrote:...As an example, I would like to download classes containing methods regarding Statistics (Probabilities, Cumulative Distribution Function, etc.) and import them on my code.


The word you are looking for is Jars not classes.
You can look at https://mvnrepository.com/ for open sourced libraries. Probably Apache math or Combinatorics is what you are looking for. Not sure though.



And where should I download famous Jar files, as the one relative to Math class?
 
Stephan van Hulst
Saloon Keeper
Posts: 12811
278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You mean "standard API classes". You don't need to download them, they are included in the Java installation. If you use the correct import statement, it's enough*.

*usually. There are some exceptions to do with enterprise applications, but they are not relevant nor exciting to you yet.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Claud Mann wrote:Do you know which are the most famous websites where I can download these Jar Files containing written classes?

As an example, I would like to download classes containing methods regarding Statistics (Probabilities, Cumulative Distribution Function, etc.) and import them on my code.
I don't know where to search them.


Googling something like java statistics library brings up some possibilities.  The Apache software is usually a good bet (I haven't used their Math library, but I've used other libraries of theirs.)  To look at what their library provides, see this URL:

http://commons.apache.org/proper/commons-math/javadocs/api-3.6.1/index.html
 
Saloon Keeper
Posts: 23409
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

salvin francis wrote:Knowing C programming definately helps. I was a C programmer almost a decade ago !! It would be a different experience to work with object oriented programming. There's a learning curve to everything but loops and conditions and switch statements would look familiar. I also found that using references in Java was a very intuitive especially knowing how pointers work.



Up to a point. The early Java certification exams were full of questions designed specifically to trip up people who tried to apply the same rules to Java as what they'd learned with C/C++. And actually, one of the more civilized things about Java is that "pointers" in the literal C sense, don't exist in Java. There are 1001 ways to slaughter yourself with pointers in C++, and, alas, I've been encountering many of them this week. In Java, you just say "here. This thing". No "." versus "->" versus "&", fewer surprise failures due to massively mis-cast data types, no bizarre behaviours as random bits of memory get fetched via un/improperly-initialized pointers. And so forth. Java. "C++ without the mace and knives". (Alan Kay, I think).
 
Tim Holloway
Saloon Keeper
Posts: 23409
159
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
One thing to bear in mind is that Java does not have header files like C++ does. Thanks to the magic of the build system, the class file itself serves as its own header, eliminating dual-maintenance problems. While Interface files may look like headers, but they are actually closer to Windows OLE/COM interfaces - a contract saying that a specific class instance will honor a specific set of method calls - in addition to possibly other ones external to the Interface definition.

The Eclipse IDE features an "optimize imports" feature where you can tell it to crunch down a set of import statements in a class definition to make it tidier.
 
Knute Snortum
Sheriff
Posts: 7111
184
Eclipse IDE Postgres Database VI Editor Chrome Java Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ctrl-Shift-O will optimize imports in Eclipse; Ctrl-Alt-O will do it in IntelliJ IDEA, although the effect is slightly different than Eclipse.
 
The City calls upon her steadfast protectors. Now for a tiny ad:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic