0

I am confused with the terminology and its use in the eclipse IDE.

It is my understanding that you can use the import keyword to reference specific classes/java files within a group of class/java files called packages. You can also reference an entire group of classes/java files by using a wildcard modifier end the end of the import statement. So why do this when you can just make the package statement at the top of the java src file instead of having to do an import?

for instance:

folder structure: myapp>graphics>.class1, .class2

instead of doing this: import myapp.graphics.*

we can do this right?: package myapp

so whey even have the import keyword? can't you just use package myapp and reference the class with say class1 example = class1();

Second, I tried to remove the package com.example.myapp and just import the classes with import myapp.graphics.* but it it gave me an error stating that "" does not match the expected package com.example.myapp. one of the fixes for it was to move MainActivity.java to a default package. I chose that option and it moved it to a default package and then I was able to use the import myapp.graphcis.*; statement while leaving out the package myapp.graphics statement without any errors.

I am confused. Also, what is a deauflt package? I read somewhere that it is bad practice to use a default package. why?

thanks.

  • 1
    http://docs.oracle.com/javase/tutorial/java/package/packages.html –  Feb 14 '14 at 12:23
  • I read it and it says that the package keyword is used only to tell the compiler that the current java file that you are editing is in the mention package and you use the import keyword to import the class or classes within that package. Am I correct? @Nikhil Joshi you statement conflicts with this explaination though. but, You are saying you use import to only import pre-existing classes. – user3292196 Feb 14 '14 at 13:13

2 Answers2

0

According to oracle:

A package is a namespace that organizes a set of related classes and interfaces. Conceptually you can think of packages as being similar to different folders on your computer. You might keep HTML pages in one folder, images in another, and scripts or applications in yet another. Because software written in the Java programming language can be composed of hundreds or thousands of individual classes, it makes sense to keep things organized by placing related classes and interfaces into packages.

So, you can think of packages as organizers, which have nothing to do with imports.

The default package is no package at all. Eclipse warns you about this when you leave the field blank. Using it is bad practice because

Using the default package may create namespace collisions. Imagine you're creating a library which contains a MyClass class. Someone uses your library in his project and also has a MyClass class in his default package. What should the compiler do? Package in Java is actually a namespace which fully identifies your project. So it's important to not use the default package in the real world projects.

Why shouldn't we use the (default)src package?

Community
  • 1
  • 1
GreySwordz
  • 368
  • 1
  • 9
  • please use `>` for quotes and backticks or 4 spaces indent for code –  Feb 14 '14 at 12:30
  • 1
    If it answered your question please mark it as solved ;) – GreySwordz Feb 14 '14 at 13:16
  • The statement that packages have nothing to do with imports is not correct. Packages are very important to imports, as they help determine what needs to be imported and what does not. – E-Riz Feb 14 '14 at 13:49
  • Rather packages are a path to the class which you are importing. What I meant by that statement is that you cannot import something by declaring a package. – GreySwordz Feb 14 '14 at 14:16
  • I'm confused.Can you import both .java and .class files with the import keyword?lets say i have my main source code .java file in the com.example.myapp; folder and also a custom class in a file called custom.java in that same directory. so i have to first do `package com.example.myapp;` then import the class using `import com.example.myapp.custom;`? how do you use custom classes in your project.I know that you can directly type the class code into the .java source file.Or you can create a new class by using New>class from the package explorer context menu.How do you reference external classes? – user3292196 Feb 15 '14 at 07:15
  • Think of packages as the separators for all of your classes; it helps you keep organized. You declare which "folder" you want to put the class file you're writing by saying "package com.example.myapp". Now, say you want to use an object you've created called "MyObject" which has the project declaration "com.example.objects". To import it you do "import com.example.objects.MyObject". To use classes someone else has made, you need to use an API, which you can use by downloading it, then right clicking on your project name and doing properties>external jar files. Then you can select one to use. – GreySwordz Feb 15 '14 at 14:29
0

Packages - A location where class is located. It is used to build the fully qualified class name.

Import - Used for incorporating pre-existing classes, by using this you can avail the functionality from them.

Default package - Location directly under src folder, for example: src\NoPackage.java

For example, if NoPackage.java contains import com.assertcheck.AssertTesting; then AssertTesting is references under NoPackage class.

However in AssetTesting class you can't import/use NoPackage.

Nikhil Joshi
  • 817
  • 2
  • 12
  • 34