Making a package default like java.lang in java - java

what I mean by making a package default is including the java package into the class mainstream just like java.lang
suppose I Have a package utility.* I want this package to be imported by default in any class I create.
Hence, whenever I'd create a class, I wouldn't have to write the import statement at all, as we don't have to in the case of java.lang. Is there a way to achieve this?

Only java.lang is auto-imported this way and there's no way to add other packages to this list (outside of building your own version of Java, which is definitely a bad idea).
However, most IDEs allow you to define "favorite" packages that will be searched first for suggestions when writing a class name that hasn't been imported there and adding your package to that list has a very similar effect, since the IDE will just add the import statement for you.

Related

Import Statement doen't showing the class file in JAR [duplicate]

I'm working now together with others in a grails project. I have to write some Java-classes. But I need access to an searchable object created with groovy. It seems, that this object has to be placed in the default-package.
My question is: Is there a way to access this object in the default-package from a Java-class in a named package?
You can’t use classes in the default package from a named package.
(Technically you can, as shown in Sharique Abdullah's answer through reflection API, but classes from the unnamed namespace are not in scope in an import declaration)
Prior to J2SE 1.4 you could import classes from the default package using a syntax like this:
import Unfinished;
That's no longer allowed. So to access a default package class from within a packaged class requires moving the default package class into a package of its own.
If you have access to the source generated by groovy, some post-processing is needed to move the file into a dedicated package and add this "package" directive at its beginning.
Update 2014: bug 6975015, for JDK7 and JDK8, describe an even stricter prohibition against import from unnamed package.
The TypeName must be the canonical name of a class type, interface type, enum type, or annotation type.
The type must be either a member of a named package, or a member of a type whose outermost lexically enclosing type is a member of a named package, or a compile-time error occurs.
Andreas points out in the comments:
"why is [the default package] there in the first place? design error?"
No, it's deliberate.
JLS 7.4.2. Unnamed Packages says: "Unnamed packages are provided by the Java SE platform principally for convenience when developing small or temporary applications or when just beginning development".
In fact, you can.
Using reflections API you can access any class so far. At least I was able to :)
Class fooClass = Class.forName("FooBar");
Method fooMethod = fooClass.getMethod("fooMethod", String.class);
String fooReturned = (String)fooMethod.invoke(fooClass.newInstance(), "I did it");
Use jarjar to repackage the jar file with the following rule:
rule * <target package name>.#1
All classes in the default package of the source jar file will move to the target package, thus are able to access.
You can use packages in the Groovy code, and things will work just fine.
It may mean a minor reorganization of code under grails-app and a little bit of a pain at first, but on a large grails project, it just make sense to organize things in packages. We use the Java standard package naming convention com.foo.<app>.<package>.
Having everything in the default package becomes a hindrance to integration, as you're finding.
Controllers seem to be the one Grails artifact (or artefact) that resists being put in a Java package. Probably I just haven't figured out the Convention for that yet. ;-)
just to complete the idea:
From inside default-package you can access objects resided in named packages.

confused with package and import statements including default package

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.
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.
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?

Can't import Java class?

The screenshot is the error I am receiving. I am trying to use the class ConnectDB contained within the package CIS4362Connect1, in the class AdminManager in the package myconnectoracle. Why can't I import this class?
As comments above indicate, it is possible that your ConnectDB is not public, in order to use that close outside of the its package, it must be declared public
Please refer to: Access control documentation
EDITED: (Solution)
The problem is that packages are case sensitive, you have a different case in both packages declaration.
inside the ConnectDB class you maybe have cis in lower case.
As far as I can tell, the class you are looking for is not defined in the current project. Try referencing the project with the package you are looking for in the libraries, or just move the package/class.
I just got a very similar issue with one of my class being unexplicably unimportable. I tried building the project anyways, and it did build successfuly, despite errors showing everywhere.
Moving the package to another location within my ide, and then moving it back to where it was supposed to be located somehow solved the issue for me.

Jars with default package

I found this question -> Import custom libraries in Java
And #Andy Thomas-Cramer said that the classes in "stdlib.jar" from "An introduction to programming in Java" have no packages, so they are in the default package.
Isn't this a bad practice? If you have something with no package the IDEs' auto-completion is quite slower. And also this means that we could not use any of the classes, in that jar, from classes with packages different then the default?
Can someone please tell me how we could deal with this?
EDIT:
I have 2 jars and I put them in Referenced libraries, they both have a bunch of classes in default package. When I create class in different package then the default - lets say org.myquestion I can't access the classes from the jars anymore.
This is something that really bugs me... First I can't create my own package and use anything from the jars. Second my IDE's (I use eclipse) auto-complete goes terrible - I guess it searches to meany classes at once... What I want to do is to put somehow the jars in some namespace... and to be able to access them like org.someones.libs.SomeClass
It certainly is bad practice to use the default package. A package groups classes and provides them with access protection (protected, package private) and functions as a unique namespace.
You can always use classes from every package, them being default or not, you can always mix.
Download the jar source code, And built it to jar by yourself and added the package name whatever your like.That's will solve your problem.
Importing classes inside JAR files that are in the default package
I ran into the exactly same problem as you did. The problem is the jar file "stdln.jar" has no named package, say, only with default package.
You cannot import a class from a default package, basically, since the import operation needs the package name:
import packagename.*;
So there are only two way to fix this problem:
the easier one: Do not create a package in your src folder and use default package two! Every class in stdln.jar would be imported to your src automatically.
Like this:
enter image description here
try to create your own jar file with a named package and copy all the class file into your newly-created jar file.
Since the stdln.jar is only used for education, so which you are gonna choose does not really matter. In real development, we never use default named package since it's not really a good practice, always leading to some confusing stuff.
Hope this would help you!

accessing referenced libraries from packages in eclipse

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.

Categories