Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
94 views
in Technique[技术] by (71.8m points)

java - Instance isn't passsing given variables to class

first of all; Thanks for reading and helping me out, I'm new to Java I ran into a problem.

I've made instance variables inside a particular class. After this I create a new instance/object of this class and specify the new input of the variable. The problem is: when I run the code and try if its working, it isnt working. It appears that the new variable is not passed to the class being instantiated. (When I left click the emerald block an empty string appears)

What I've already tried:

  • creating a setter method to set the variable.
  • using a constructor

Class that I want to make an instance of:

package rico.polkadot;

import org.bukkit.Material;
import org.bukkit.block.Block;
import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler;
import org.bukkit.event.Listener;
import org.bukkit.event.block.Action;
import org.bukkit.event.player.PlayerInteractEvent;

public class InstanceClass implements Listener {
    
    public String name;     
    
    @EventHandler
    public void onInteract(PlayerInteractEvent e) {
        
        Action action = e.getAction();
        Player p = e.getPlayer();
        Block b = e.getClickedBlock();
        
        if(action.equals(Action.LEFT_CLICK_BLOCK)) {
            if(b.getType().equals(Material.EMERALD_BLOCK)) {
                p.sendMessage(name);
            }
        }
    }
}

Main class:

package rico.polkadot;

import java.util.List;

import org.bukkit.plugin.java.JavaPlugin;

public class Main extends JavaPlugin {
    
    @Override
    public void onEnable() {
        getServer().getPluginManager().registerEvents(new InstanceClass(this),this);
        InstanceClass test = new InstanceClass();{
            test.name = "lol";
        }
    }
}
question from:https://stackoverflow.com/questions/66064936/instance-isnt-passsing-given-variables-to-class

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok, you should make a constructor or at least setter in your InstanceClass - best option:

public class InstanceClass implements Listener {

    public String name;

    public InstanceClass(String name) {
        this.name = name;
    }
}

Then you are asking about instance. You created one inside your onEnable() function, but you have to remember, it is not accessible outside of this function, so you can't just access this test variable from your InstanceClass.

@Override
public void onEnable() {
    // here you create instance of InstanceClass
    InstanceClass test = new InstanceClass("lol");
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...