Can I put a java package into another package? - java

Using Netbeans, I want to put a package into another package. For example in Visual Studio 2008, I can have folder called "Nodes", and another folder inside of Nodes called "Expressions". How do I do this in Netbeans? I think a package in Java is equivalent to a folder in C#.

For a package within a package, put the parent name, a period, and then the name of the child's package like so: Nodes.Expressions.
It will appear as it's own separate package in an IDE perhaps, but the folder hierarchy will be as you desire: Nodes/Expressions/[classes etc]

You can create subpackages in java. If your package is called nodes, adding an expressions folder inside of it will create a nodes.expressions package.
FYI in Java, it is customary to use all lower case for package names.

Kevin is correct in his answer about packages.
Heres Netbeans specific steps for adding a new package:
In your Projects view, go to the parent package under "Source Packages".
Right click on the Package and select New>Java Package (if Java Package doesn't appear in the list, select Other... and then pick Java>Java Package)
Fill out the New Java Package wizard with name of the child package

You can have any number of subpackages, e.g. abc.def.ghi.jkl.

You can simply put the source in a folder within the current package structure. You reference it by adding a dot and the new package name to the end of the existing package name. In Netbeans, in the new file wizard, where the package name is referenced, you can input the new package by choosing an existing package and adding the .newpackagename to the end. Netbeans will then create the directory structure for you.

Related

How do I correct the package declaration for a set of classes under the same package in Eclipse?

I am moving a set of classes from one package structure to another.
Say I am moving the classes from com.oldstruct.oldpack to com.newstruct.newpack.
Is there any way to automatically change the package structure declaration for all the classes to the new package (com.newstruct.newpack) in Eclipse?
In package explorer right click on the package, choose refactor->rename type in com.newstruct.newpack.
In project explorer just drag and drop the classes from one package to another.Eclipse will automatically correct the package declarations for all those classes.You can also refactor and rename the package name .Refactoring will automatically update all the references.

Configuring IntelliJ project

My problem is very simple to describe.
I've made a Class named Car and I let IntelliJ build the Car.class file.
After that I've made another Project which has a dummy class called Main which wrapps this Car object.
In the project structure I've added as an external library the folder which contains Car.class.
So basicly after declaring the Car member variable in my Main dummy class, IntellJ normaly suggests me to import Car which is shown in the picture below.
Picture1
When I choose to import Car class it directly writes down the package before the Class name (not the usual import statement) and the import fails as shown on this picture.
Picture2
(note: the reason it states org.dino.test.pojos is because Car class package is org.dino.test.pojos)
The project setup is shown below.
Picture 3
Does someone know how to setup this project structure so such things work?
The fact that IntelliJ has marked the org directory as the library home is suspicious- I suspect that you have defined the classes in the library as the org subdirectory rather than the JAR or classes directory itself.
You also have a folder structure that is inconsistent with your package structure.
When you define the library, select the testLibrary directory/JAR rather than one of the directories inside it. Also, ensure that the folder structure inside your library matches the Java package of the class within it.

Java package in package?

I'm using eclipse 3.8 indigo and I don't know why, when I'm creating a new package the destination of the new package goes outside. I want to create new packages within a package: package in package. I tried to copy / paste the newly created package or to move, but it just copies.
For example, I want to create different packages: dialogs, views, tables, etc. in my main source package. For example:
com.new.application // this is the the main package created by wizard
com.new.application.view // package that contains all views.
In reality there is no such thing as a sub-package in Java - each package is a completely separate entity, with the names being seemingly hierarchical only for convenience. For example, items with default visibility are not visible in sub-packages, despite what one might expect.
If your problem has more to do with presentation and aesthetics than substance, then perhaps what you are looking for is the hierarchical package presentation setting in the Eclipse Package Explorer: click on the little downward triangle/arrow at the top right of the package explorer and select "Hierarchical" in the "Package presentation" submenu:
This is a global setting and will affect all your opened/un-opened projects.
There is no concept of package with in package. Each package is separate namespace. I think if you go to folder view instead of package view, there you may see one under another.
The eclipse package explorer has two view options: flat or hierarchical. You are probably in the flat view (which is the default, who knows why). Change the view to hierarchical by clicking on the small triangle on the top right corner of the package view and then changing the package representation.
Firstly, you should select hierarchical representation.
Secondly, you should entitle completely package name. For example, you have 'main' package and you will create 'sub' package, you should entitle this with 'main.sub'. If you have just 1 package in package, may be you can't see hierarchicaly these packages.
After that, package view:
Second sub package creation:
Finally package view:

Java Package Vs Folder-Structure? what is the difference

I would like to know What are the difference between folder-structure and package used in Eclipse IDE for Java EE development.
When do we use which one and why?.
Whats should be the practice
create a folder structure like src/com/utils and then create a class inside it
create a package like src.com.util and then create a class inside it
which option would be better and easy to deploy if i have to write a ant script later for deployment ?
if i go for the folder-structure will the deployment is as easy as copying files from development to deployment target ?
If you configured stuffs correctly. Adding a folder inside src, is same as adding a package from File > New Package.
So, it's up to you, whatever feels comfortable to you -- add a folder or create a package. Also, when you put stuffs under src the package name starts from subfolder. So, src/com/naishe/test will be package com.naishe.test.
Basically there is no difference, both are the same.
In both the cases, the folder structure will be src/com/utils.
and in both the cases, you will need to mention
package com.utils;
as first line in the class
Since it doesn't have any difference practically, it won't make any difference to ant script.
"Packaging helps us to avoid class name collision when we use the same class name as that of others. For example, if we have a class name called "Vector", its name would crash with the Vector class from JDK. However, this never happens because JDK use java.util as a package name for the Vector class (java.util.Vector). So our Vector class can be named as "Vector" or we can put it into another package like com.mycompany.Vector without fighting with anyone. The benefits of using package reflect the ease of maintenance, organization, and increase collaboration among developers. Understanding the concept of package will also help us manage and use files stored in jar files in more efficient ways."
check out http://www.jarticles.com/package/package_eng.html for more information on packages
create a package like 'src.com.util'
That sounds like a mistake. The package name should be 'com.util', and 'src' is the name of the source folder.
Other than that, I fail to see what the difference is between your two choices. The result is the same, right? Just different steps in the GUI to arrive at it. The wizard to create a new package in Eclipse is just a wrapper around creating the appropriate folder hierarchy within a source folder.
You don't need to create empty packages at all, you can directly create classes (the package will be created automatically if it does not already exist).
A package is automatically "source folder" where folder is just a normal folder.
When you compile an Eclipse project, all files in source folders are compiled but not in regular folders (unless those regular folders a)
folder structure or to be specific source folder in eclipse is meant just for eclipse but package is universal irrespective of any editor..

Java package convention

I'm developing an Android project which currently has 4 packages:
com.myapp.app.activities
com.myapp.app.db
com.myapp.app.ws
com.myapp.app.utils
Would I be able to create an additional package which is just
com.myapp.app
?
Eclipse isn't letting me create this package. It tells me a package with this name already exists.
If I start a new project and create a package called "com.testing.app" and then create a new package called "com.testing.app.activities" afterward, it works fine.
For Android developers:
What I'm wanting to do is extend the Application class and have it in a separate package. Suppose com.myapp.app can't be used, what's a good name for this new package?
Eclipse won't let you create this package because it already exists.
Packages in Java are represented in the filesystem as hierarchical folders: com.myapp.app.activities is in the com/myapp/app/activities folder. com/myapp/app already exists, so you can't create this package.
In Eclipse, juste create a new class, and in the "Package" section, precise you want to create it in the com.myapp.app package. This should work.
The package com.myapp.app already exists. You can create a class named com.myapp.app.MyClass, you'll see it right in the app package.
Another thing you can do is changing the layout of your packages from a flat layout to a hierarchical layout :
Resources :
help.eclipse.org : Project Explorer view
Eclipse by default hides empty packages. In the package explorer view, click at the small arrow in the right top: View Menu. Choose Customize View. In the Filters tab you need to uncheck Empty packages. Now empty packages will be visible in the package explorer.
It would appear that if I manually place a file into the directory and refresh the package explorer in Eclipse, the new com.myapp.app package appears

Categories

Resources