Why do I get package name in the top of the page whenever I try to create a java class?
I have written some codes in the past using Eclipse but it had no package name like this. I am just wondering why?
You can see the following.
Your class in contained in the package named asd. This is also shown on the left hand side: project "asd" / package "asd" / Class "Ex1.java"
If you want to get rid of the package, you need to move your class to "default package". Right click on the class -> Refactor -> Move -> choose (default package)
However, I would recommend to work with packages and name them properly. Putting classes into "default package" is considered bad practice.
As you can see in package explorer
you placed your class inside asd package. This happened when you created your class via File->New->Class wizard. If you don't specify any package then by default it will use project name.
So when you confirm such configuration such setup Ex1.java will be placed inside asd folder (package), which means that your code will need at its start information about name of package.
If you don't want to have this package asd; line you can for instance
clean that section from wizard when you create package,
or even remove that line from your code and when Eclipse will ask you how to fix this situation
place cursor in line with problem (first line)
select Edit->Quick fix, or press Ctrl+1)
and chose Move Ex1.java to default package.
But you should avoid using default package. For instance when you export classes from your project to Jar file you will not be able to import them in new projects.
For more info about packages visit official tutorial:
What Is a Package?
Lesson: Packages
All Java classes have specified their address in the beginning of the document. They say
package package_name;
If the class is in the default package (directly in 'src' folder), then there is no package written in the beginning of the class...
You can see here, and try on your own:
Related
I installed intelliJ and created a package of name "com.company" and created some classes and interfaces in that package. Now I created a new package "myCalculator" and created some classes as well. But now I see my new package is not having the main class, so I need to import the package manually. How do i bring my main class to my new package in IntelliJ. Thank You.
You can just drag your files from package to package. Behind the scenes Java packages are just folders, so its is just the same as moving files between folders.
Use refactor option.
Select a class of a package
right click on it and select refactor
choose move option and enter the desired destination package name
To better organize my classes, I made a few sub-packages (that is an extension in the package's hierarchical name structure).
But to my surprise I have been getting an R cannot be resolved to a variable for like every resource I am trying to use from this project's R file.
The Eclipse's quick fixes options do give the option of importing the particular R file for the package (which contains the sub-packages), But the question is that in the package that I created in the Eclipse's New Application Project wizard, I did not need to import the R file at all, and I always thought it contained the resources for this entire project.
But now after creating sub-packages, I need to import it. SO asking out of curiosity, since a sub-package is nothing (in Java) but an extension in the hierarchical name structure of the package itself (reference), then why doesn't the R file automatically get included in the sub-packages?
R is just a class like any other class (except that Eclipse creates it for you and keeps it updated with your resource IDs).
It is contained in a package - like every other class.
If you want to use it from a different package, you must import it or use the fully qualified name - like you would with any other class.
Someone help me please? I don't know why can't I run a simple program by the run configuration. If I don't set a package in the beginning it works.
I attach a picture of my screen
The class bb is in the package aa2. Its name is thus aa2.bb, not bb.
If not specified the package name externally then a given class is said to be included in default package i.e this class exists directly under src folder.
If you remove the package statement in the class then you have to move this class to one folder level up, in order to run this class.
But it is discouraged and normally it is expected that each class should declare its own package statement.
when I create a new java project in intellij the module is: com.example.ProjectName .
how can I use my own domain?
like:
us.mysite.ProjectName
I've already tried to look for it and didn't found the answer.
This is the name of the java package. In order to change the package, the package foo.domain.project line must be changed along with the name of the directory in which the code lies. As seen here packages can be created with varying names through IntelliJ(including possibly when creating a new project), or a file can change:
package foo.domain.project;
to
package bar.dom.proj;
and move sources from the src/foo/domain/project directory to the /bar/dom/proj directory.
There is lot of useful information available on Netbeans refactoring wiki. Here is how you can rename your package
Step1 : Right-Click on the package name, in the Refactor menu select
Rename (Ctrl+R)
Step2 : In “New Name” enter the new name that you desire for the
token. Then click on Refactor button complete the operation.
Optionally you preview the effect of the refactoring.
I can only access referenced library classes if I save my classes in the default package. If I try to access them from any other package I get "className cannot be resolved". Any idea why this could happen?
That package is from the standard library of Princeton's IntroCS Course after a quick Google.
If you follow down to the FAQ on the page
http://introcs.cs.princeton.edu/java/stdlib/
Q. If I use a named package to structure my code, the compiler can no
longer access the libraries in stdlib.jar. Why not?
A. The libraries
in stdlib.jar are in the "default" package. In Java, you can't access
classes in the default package from a named package. If you need to
use our libraries with a named package, you can use the packaged
version stdlib-package.jar.
Download package jar file:
http://introcs.cs.princeton.edu/java/stdlib/stdlib-package.jar
Right click project folder and add external JAR.
import edu.princeton.cs.introcs.*;
Add above line to the classes where you need to reference the classes.
The first line references the correct package name and the * wildcard imports all the classes within it.
:) Hope that helps.
//Edit - If you right click the project folder and use "Organize Imports" it will be faster so you don't have to manually add to each class.
Check if you've proper import declarations and the type you are refering to has public access modifier.
Import declaration for Types in the default package are per definition impossible. JLS specifies that it is a compile time error to import a type from an unnamed package.
You must access your class via reflections or much better do not ever use the default package. Eclipse should show you a warning when you want to create a type inside default package, because it's generally discouraged.
If you're using an IDE like Eclipse then try hitting CTRL+SPACE behind the type name in the class where you want to use it. Eclipse should give you all matching opportunities and will add the import automatically for you if you select your class.
You need to import the package into other package.
Classes in one package cannot directly reference to other package unless its get referenced.