arraylist list not being recognized in another class - java

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.

Related

whats the difference between import a class and do an association in java to use their methods

Here we have a class code of a chess game. i can see that when we need to use methods of another class, we can do an association, like the class ChessMatch as we can see. but the question is:
why a need to do an association with another class to use their methods while i can just import a class?
package chess.pieces;
import boardgame.Board;
import boardgame.Position;
import chess.ChessMatch;
import chess.ChessPiece;
import chess.Color;
public class Pawn extends ChessPiece {
private ChessMatch chessMatch;
/*CONSTRUTOR*/
public Pawn(Board board, Color color, ChessMatch chessMatch) {
super(board, color);
this.chessMatch = chessMatch;
}
.
.
.

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.

How do I fix "non-static method cannot be referenced from a static context"? [duplicate]

This question already has answers here:
Non-static variable cannot be referenced from a static context
(15 answers)
Closed 5 years ago.
My Java compiler returns the following error message
non-static method getParameter(String) cannot be referenced from a static context
on the line String Cliente4 = UtilMainApp.Cliente.
I have the following classes: UtilMainApp.java
package uk.co.mmscomputing.util;
import java.io.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.beans.*;
import javax.swing.*;
import uk.co.mmscomputing.util.log.LogBook;
abstract public class UtilMainApp extends JApplet{
private Properties properties=new Properties();
private File propertiesFile;
private JFrame frame = null;
public String Cliente = getParameter("id"); // I need use this variable
... etc...
and ImageTab.java:
package uk.co.mmscomputing.application.imageviewer;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.awt.geom.*;
import java.awt.image.*;
import java.awt.print.*;
import java.io.*;
import java.util.*; // as of 1.5.0 java.util has class Scanner
import javax.imageio.*;
import javax.imageio.stream.*;
import java.beans.*;
import java.net.URL;
import java.net.URLConnection;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import javax.imageio.metadata.*;
import uk.co.mmscomputing.util.UtilMainApp;
import uk.co.mmscomputing.util.JarImageIcon;
//import uk.co.mmscomputing.imageio.*;
import uk.co.mmscomputing.image.operators.*;
public class ImageTab extends JPanel implements PropertyChangeListener{
static public final String fileOpenID="uk.co.mmscomputing.file.open.dir";
static public final String fileSaveID="uk.co.mmscomputing.file.save.dir";
protected Properties properties;
protected JTabbedPane images;
protected JFileChooser openfc;
protected JFileChooser savefc;
... etc...
//UtilMainApp mc = new UtilMainApp(); //This is the part when I compile have an error
String Cliente4 = UtilMainApp.Cliente;
String Usuario;
Usuario = Cliente4;
JOptionPane.showMessageDialog(null, Usuario);
...etc...
How is this caused and how can I solve it?
String Cliente4 = UtilMainApp.Cliente;
It seems that Cliente is a Non-static Variable, so it must be accessed by making an object of UtilMainApp Class.
Eg:
UtilMainApp util = new UtilMainApp();
String Cliente4 = util.Cliente;
The error is here, where you are referencing a non-static variable in a static way. UtilMainApp is a class, not a property. So you're referencing Cliente as if there is only one of them per class (static) when it is not defined that way.
String Cliente4 = UtilMainApp.Cliente;
To fix this problem, you have to pass an instance of UtilMainApp into the constructor for your ImageTab class, so that you will have the reference to use to get the non-static property Cliente. I'll also note that this property cannot be set to static since it is initialized through the non-static call to getProperty() in the super class.
public String Cliente = getParameter("id"); // I need use this variable
One other note - it is good practice to name all properties beginning with lowercase letters. This is to make sure that you confuse class names with property names. Your code is quite difficult to follow because you do not follow this convention.

Importing packages in 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;"

Java : The import collides with another import statement

I have imported an Existing Java Application into my Workspace .
I see that , a class with same name is present in different packages with in the Application.
For example a class named "Status.java" is present with in
com.tata.model.common.Status;
com.bayer.frontlayer.dao.Status;
When I tried to use both of them within a class, for example as shown below
import com.tata.model.common.Status;
import com.bayer.frontlayer.dao.Status;
public class Adapter
{
}
It started giving an error in Eclipse stating
The import com.bayer.frontlayer.dao.Status collides with another import statement
Is there anyway to solve this without changing the name of the classes??
Thank you.
You can use them explicitly without importing them, so the included package name differentiates between the two:
//No imports required!
public class Adapter
{
private com.tata.model.common.Status x;
private com.bayer.frontlayer.dao.Status y;
}
You can import just one of the classes and use the fully qualified name for the other one.
e.g.
import com.tata.model.common.Status;
//import com.bayer.frontlayer.dao.Status;
class SomeClass{
void someMethod(){
new Status(); // com.tata.model.common.Status
new com.bayer.frontlayer.dao.Status(); //com.bayer.frontlayer.dao.Status
}
}
Though I think it would be less confusing in your case if you just used the fully-qualified names for both classes.
Directly apply full Class Names wherever applicable. Eg-
public class SomeClass {
public someMethod() {
com.myapp.someotherpackage.Status = "something";
com.some.other.package.Status = "otherthing";
if(com.myapp.someotherpackage.Status == com.some.other.package.Status) {
}
....
}
}

Categories