Importing packages in Java - java

How to import a method from a package into another program? I don't know how to import... I write a lil' code:
package Dan;
public class Vik
{
public void disp()
{
System.out.println("Heyya!");
}
}
and then, saved it in a folder named "Dan" and I compiled it. The .class file is generated. Then, I wrote this code below:
import Dan.Vik.disp;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
and I saved it outside the folder "Dan" and it says : "cannot find symbol"
I saved the first code in C:\Dan\Vik.java
and the second in C:\Kab.java

You don't import methods in Java, only types:
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
The exception is so-called "static imports", which let you import class (static) methods from other types.

In Java you can only import non-primitive types, or static methods/fields.
To import types use import full.package.name.of.TypeName;
//example
import java.util.List; //to import List interface
to import static methods/fields use
import static full.package.name.of.TypeName.staticMethod;
import static full.package.name.of.TypeName.staticField;
//example
import static java.lang.Math.max; //to import max method(s)
import static java.lang.Math.PI; //to import PI field

Take out the method name from in your import statement. e.g.
import Dan.Vik.disp;
becomes:
import Dan.Vik;

You should use
import Dan.Vik;
This makes the class visible and the its public methods available.

Here is the right way to do imports in Java.
import Dan.Vik;
class Kab
{
public static void main(String args[])
{
Vik Sam = new Vik();
Sam.disp();
}
}
You don't import methods in java. There is an advanced usage of static imports but basically you just import packages and classes.
If the function you are importing is a static function you can do a static import, but I don't think you are looking for static imports here.

In Java you can only import class Names, or static methods/fields.
To import class use
import full.package.name.of.SomeClass;
We can also import static methods/fields in Java and this is how to import
import static full.package.nameOfClass.staticMethod;
import static full.package.nameOfClass.staticField;

For the second class file, add "package Dan;" like the first one, so as to make sure they are in the same package; modify "import Dan.Vik.disp;" to be "import Dan.Vik;"

Related

arraylist list not being recognized in another class

I was creating a plugin to MineCraft which needs a list of UUID and I figured to do it this way
public class Freeze extends JavaPlugin implements CommandExecutor {
public static List<UUID> toggleList = new ArrayList<UUID>();
}
However when I'm using the list in another class it says cannot resolve symbol.
Here is the class for using the list not creating it
import org.bukkit.Location;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.entity.Player;
import org.bukkit.event.player.PlayerMoveEvent;
public class Toggle implements Listener {
#EventHandler
public void onPlayerMove(PlayerMoveEvent evt) {
Player player = evt.getPlayer();
if (Freeze.togglelist.contains(player.getUniqueId())){
Location back = new Location(evt.getFrom().getWorld(), evt.getFrom().getX(), evt.getFrom().getY(), evt.getFrom().getZ());
evt.getPlayer().teleport(back);
}
}
}
how can i get it to recognize it as the list?
Static import
Add a static import statement to your Toggle class.
import static my.package.Freeze ;
Or change your line of code to use the fully-qualified name.
if ( my.package.Freeze.togglelist.contains( player.getUniqueId() ) ) {
And if working with retrieved UUID objects, you will need to import that class as well.
import java.util.List ;
import java.util.UUID ;
No Static Import Required
The reason why your IDE is barking an error is because of a typo. You define toggleList in Freeze, but attempt to reference it with Freeze.togglelist (note that "list" is spelled with a lowercase 'l').
If your classes are in the same package, no further import directives are required. However, if Freeze is in a different package, a regular import (i.e., non-static) of Freeze is required, which is something your IDE should be able to resolve easily.

importing a final class in Java

I have declared this class:
public final class Constants {
public static final String PROFILE_PCT__FILTER_NAME_ID = "profilePct";
}
I want to use that class in another class with..
import static com.tdk.utils.Constants.*;
But I got this compilation error:
Constants cannot be resolved to a variable
When you are using the static import with *, you have access to the variables defined in the class. However, you can't refer to the class itself.
Should work:-
import static com.tdk.utils.Constants.*;
String a =PROFILE_PCT__FILTER_NAME_ID;
Shouldn't work:-
String a =Constants.PROFILE_PCT__FILTER_NAME_ID;
You need the below import to resolve the compilation issue.
import com.tdk.utils.Constants;

Unable to do static import

//interface1.java
package package1;
public interface interface1 {
static final int a =10;
}
//StaticImportTest.java
import static package1.*; //import package1.*; works
class StaticImportTest {
public static void main(String args[]) {
System.out.println(a); //System.out.println(interface1.a) works
}
}
when i am replacing the word "import static" with only "import" and using System.out.println(interface1.a) it works, but not sure why its not working in its current form.
For your static import to work the way you intended it would have to be
import static package1.interface1.* or import static package1.interface1.a
A static import imports public static members of a class either all with * or a specific one like for example a.
A import on the other hand imports a package or specific classes from a package.
Your import static package1.* would try to import all members from the class package1 in the root package.
Making it a normal import and accessing a via interface1.a works because the import imports all classes from the package1 including interface1, therefor you can access a via the interface1 class.

Is there any way to reduce the length of static variable from another class?

I have a public class Helper in another file that has a few public static variables declared. I'm wondering if it's possible to shorten the variable name so that I don't have to consistently use Helper.<variable_name> everywhere. It would be ideal if I could just use the variable name without the Helper prefix.
Use
import static a.b.c.Helper.<variable_name>;
where a.b.c is the package which contains the Helper class.
You can use a static import:
// Class1.java
package test;
import static test.Class2.static_var;
public Class1 {
public static void main(String[] args) {
System.out.println(static_var); // not Class2.static_var
}
}
// Class2.java
package test;
public class Class2 {
public static String static_var = "Hello";
}
Output from java test.Class1:
Hello
Wildcard imports also work:
import static test.Class2.*;
Note that you must specify the full package name in the static import declaration. It is not sufficient to import test.Class2; and then import Class2.*;.

Simple import problem

I'm trying to learn java and seem to be missing something obvious.
In subdirectory lab I have the file Play.java
package lab;
import java.io.*;
public class Play {
public static void playprint(Object obj) {
System.out.println(obj);
}
}
My CLASSPATH starts with '.'
In the parent directory I have a program
public class test {
public static void main(String[] args) {
lab.Play.playprint("hello world");
}
}
This runs fine. If I change the program to
import lab.Play.*;
public class test {
public static void main(String[] args) {
playprint("hello world");
}
}
It fails with an error that it can't find symbol method playprint
What am I missing?
To import a method you have to use import static. Without you are trying to import all classes (and interfaces) within your class "Play" only.
import static lab.Play.*;
See the documentation on static imports for details.
If you want to skip using static methods, you can create an object of Play class inside the class test and then call playprint.

Categories