Is it possible to avoid package and import statement? - java

So I learned that if I have two packages say: one default package and one another package "com.projectjava" which contains a class say, MyClass.java then I need to use a package statement inside it:
package com.projectjava;
public class MyClass {
...
}
And, in default package where the main function I need an import statement:
import com.projectjava.*;
public class MyClassTest {
...
}
Is it possible to avoid both the import and package statement and use full declaration syntax such as com.projectjava.MyClassTest? Is the package statement a requirement?

package is mandatory if your class finds itself in a package other than the default package
If you don't want to use import you may simply use the fully-qualified name of the class like for example java.util.regex.Pattern

The package statement is required, the import statement is usually recommended but not required.
If you don't use an import statement you have to use the fully qualified name of the class you're using:
public class MyClassTest {
public void foo() {
com.projectjava.MyClass myClassInstance = new com.projectjava.MyClass(...)
}
}

Related

Groovy not compiling correct class in Intellij

I have a class named "OrderBy" localized on package "br.com.petrobras.sddi.domain.".
Groovy has a class named "OrderBy" too, on package "groovy.util"
I have the class above:
//.. something
import br.com.petrobras.sddi.domain.*
// other imports
abstract class BaseJPARepository {
protected OrderSpecifier getSortedColumn(OrderBy order) {
//..something
}
}
When I compiled my program and open BaseJPARepository.class the imports contains
import br.com.petrobras.sddi.domain.FindAllPredicate;
import br.com.petrobras.sddi.domain.IEntity;
import br.com.petrobras.sddi.domain.PagedList;
import br.com.petrobras.sddi.domain.Pagination;
import com.querydsl.core.types.Order;
// others
import groovy.util.OrderBy;
// others...
So, when compiling, my class OrderBy wasn't imported.
How can I fix that? (I want to use the "*" when importing)
AFAIK, groovy loads all groovy.util.* and java.lang.* etc classes automatically. So, in order to be able to use your class you have to use it's full name in the code:
protected OrderSpecifier getSortedColumn( br.com.petrobras.sddi.domain.OrderBy order) {
//..something
}

How to properly deal with java interface in different package?

I'm new to Java and I've got a package for interfaces. Each interface has a number of imports for the types used in the interface declarations.
// MyInterface.java...
package a.b.c.interfaces;
import java.math.BigDecimal;
import java.util.List;
import java.util.Set;
public interface MyInterface() { ...
Then I've got a package with models, and inside of it, I'm attempting to create a class that implements MyInterface().
// MyImplementation.java...
package a.b.c.models;
public class MyImplementation implements MyInterface {...
I have two questions:
Does MyInterface() need to be imported into
MyImplementation()? If not, does it then need to be linked
manually during compilation?
If MyInterface() is imported, is
it also necessary to import the types the interface requires, or
does the import of MyInterface.java take care of this?
Imports in Java is basically a way of telling the compiler that when you write MyInterface you really mean a.b.c.interfaces.MyInterface. It is definitely possible to write Java code without using imports but it is extremely verbose and looks messy.
You need to import any type that you want to use that is not in the same package as the class you're writing or in java.lang so since MyInterface is in a.b.c.interfaces and MyImplementation in a.b.c.model you need to import it.
You do need to import the classes that MyInterface is using if you're writing their type anywhere in your code - and since you're implementing the interface you will need to write their types in your method declarations. There is no way of using the same imports as another class in Java.
Here is an example of usage of types where we don't need to import them:
UsesBigInteger.java:
import java.math.BigInteger;
public class UsesBigInteger {
public BigInteger get() {
return BigInteger.TEN;
}
}
Main.java
public class Main {
public static void Main(String[] args) {
UsesBigInteger use = new UsesBigInteger();
System.out.println(use.get());
}
}
The reason we don't have to import java.math.BigInteger here is that we never actually write BigInteger anywhere so we don't need to tell the compiler what we mean by that - we just pass the return value from UsesBigInteger.get() along and the compiler already knows that that is a java.math.BigInteger.
If we want to keep a reference to the BigInteger we need to import it though:
Main.java
import java.math.BigInteger;
public class Main {
public static void Main(String[] args) {
UsesBigInteger use = new UsesBigInteger();
BigInteger it = use.get();
System.out.println(it);
}
}
Ans for your first question, you have to import the interface and on class you have to implement your interface.
As for your second question, you just need to import MyInterface. The JVM will take care of the rest.
I strongly suggest you to go threw the oops concepts as its too basic question.
And also use any IDE like Eclipse.

What is the purpose of * in java.io.*

What is the purpose of the * in java.io.*?
import java.io.*;
class Trial{
public static void main(String[]args){
System.out.println("Hello,World!");
}
}
The * tells the compiler to import all top-level classes in java.io on demand. The construct is called a type-import-on-demand declaration.
From JLS ยง7.5.2:
A type-import-on-demand declaration allows all accessible types of a named package or type to be imported as needed.
TypeImportOnDemandDeclaration:
import PackageOrTypeName . * ;
So, for example, since you've included that import statement, you can use a class like java.io.File without having to prefix the type name with java.io; you can use the simple name File.
The star indicates that all classes from the java.io package should be imported.
A wildcard in a package name import is used to include all the classes contained in that specific package. Check official documentation.
In addition you are able to import inner static classes to be able to refer to them without a fully qualified name, eg:
import org.package.MyClass;
//MyClass.InnerClass inner; not needed
InnerClass inner;

Importing and Packages

I'm not quite sure what I'm doing wrong, here. I have two files in a directory, let's call them FileA.java and FileB.java.
FileA.java has a definition along the lines of:
package com.domain.package;
import stuff;
import package.FileB;
public class FileA extends Blah implements Listener {
/* global vars */
/* methods */
}
FileB.java is my data object class, which I'd like to reference from FileA.java thusly:
Map<Object, FileB> varname;
to be used along the lines of:
varname = new HashMap<Object, FileB>();
FileB.java, on the other hand, is defined as such:
package com.domain.package;
import stuff;
public class FileB {
/* global vars */
public FileB() {
/* stuff */
}
}
Why am I getting:
FileA.java:20: package package does not exist
import package.FileB;
? Rather, how do I make it work?
Because both files are in the same package (com.domain.package), you should not need to import FileB at all. You should be able to reference it directly.
Additionally, please ensure that both FileA and FileB are placed in their package folder: com/domain/package.
The package of FileB is com.domain.package. You are trying to use package.FileB instead.
package is a reserved word, don't use it as part of a package name. If you try to add a package with "package" as part of it in Eclipse, you will get an error message:
Invalid package name. 'package' is not a valid Java identifier
Rename the package, then remove the import statement. You don't need to import a file that's in the same package as the one it's referenced in.
#rgettman has the correct solution. Compiling both files using javac FileA.java FileB.javasolves this issue. You can also use his suggestion: javac *.java

Import a single class in Java

Simple question but even though googled it a lot I could not find the answer.
Is it possible to import a class outside a package?
Let's say I have 2 folders A and B with a .java file in each, is it possible by using the clause import to import the class contained in A? import A.Aclass ? or it's mandatory using package syntax whenever there is the keyword import?
Yes it is possible to import the class with the import statement. For better understanding let's assume that you have three folders foldera, folderb and folderc where foldera contains a .java file named "ClassA.java", folderb contains another .java file named "ClassB.java" and folderc contains a .java file named "ClassC.java". Now, if you want to uses the member data and operations of "ClassA.java" in "ClassC.java" you can use the import statement as shown below:
import foldera.ClassA
If you want to use the member data & operations of "ClassB.java" in "ClassC.java" it is also possible with the import statement
import folderb.ClassB
As per the java source file declaration rule, if the class is a part of a package, the package statement must be the first line in the source code file, before any import statements that may be present. In this example, the first line of "ClassC.java" source file must be package folderc since it is located in folderc. Similarly, the first line of "ClassA.java" source file must be package foldera, and the first line of "ClassB.java" source file must be package folderb.
Hope now you are clear with the concept!
Thank you...
Well, if the class is defined to have a package a; then you need to import the class with the package name. If you have two packages which contain a class with the same name, then in your class which needs to invoke each of them, you will need to use a fully-qualified name. For example:
import a.Foo;
import b.Foo;
public class Bar
{
public static void main(String[] args)
{
a.Foo aFoo = new a.Foo();
b.Foo bFoo = new b.Foo();
}
}
Alternatively, if you have two packages with classes of the same name, you can simply skip importing them, but rather -- using them by their fully-qualified names (FQN-s).
If the class does not have a package ...;, then simply import it as:
import Foo;
However, if you have two packages (from different libraries) which contain classes with identical FQN-s, then the first one on the classpath will be picked.
Please, bear in mind that the convention for naming packages is to use lowercase letters and for classes -- the name should start with an upper case letter for each word in the class' name.
Yes it is possible.
If you have the following:
Package: PackA
Class: ClasA
Do:
import PackA.ClassA; //Import the class
OR
import PackA.*; //Import all the classes within the package
yes it is possible just import the package
syntax
import pck.ClassA or import pck.*
Yes, you have to use package syntax.
importing all class inside folder A.
import com.pack.A.*;
importing specific class inside folder A.
import com.pack.ClassName;

Categories

Resources