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";
}
}
}
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");
}
Solved, needed multiple constructors and "RegisterEvents" in the onEnable. Thanks for helping.
Related
Below snippet of code is from ITDIAgentException.java file
Can some one help me understand "why class name is same as class used in import statement"(ITDIAgentException)
import com.ibm.di.entry.Entry;
import com.ibm.di.exception.ITDIAgentException;
public class ITDIAgentException extends Exception {
private Entry entry = null;
public ITDIAgentException(String paramString) { super(paramString); }
public Entry getEntry() { return this.entry; }
public void setEntry(Entry paramEntry) { this.entry = paramEntry; }
}
EDIT
You have ITDIAgentException twice: Once in the import statement, and once in the class definition. You are not allowed to have both (would create a namespace clash in the code), but you can access com.ibm.di.exception.ITDIAgentException (assuming that it is different from the class you are creating) by using the full package and class name.
import com.ibm.di.exception.ITDIAgentException;
public class ITDIAgentException extends Exception {
I'm trying to get object from another class from another package
package processManager;
public class PCB {
public int vruntime;
public int nice_value=0;
}
in the same package
package processManager;
public class Process {
public Process(PCB pcb) {
this.pcb = pcb;
}
public Process() {
}
public PCB pcb;
int a;
}
usage of object
package processManager.newpackage;
import processManager.Process.*;
public class NewClass {
public static void main(String[] args) {
Process proc=new Process();
}
}
and I don't know why but then I've got "Process is abstract; cannot be instantiated"
Please look closer at your code:
A) You have defined a class processManager.Process which is not abstract.
B) Next in the next file you are importing
import processManager.Process.*;
Which actually defines an import of all sub-classes of processManager.Process class (you have none) but the class itself is not considered an import.
C) This means that in the next piece of code
Process proc = new Process();
You are trying to create an instance of java.lang.Process class which is abstract.
This is a source of your error.
How can I use a instance with short code that is created at a class in different package ?
1.java
package a;
class First {
public Player myplayer;
public void makeContent() {
myplayer = new Player();
}
}
2.java
package b;
class Two {
public void useContent() {
First.myplayer.play();
}
}
I feel ClassName.Instance is too long to quick development
How can I resize codes what call Instance in different package
Your First class needs to be public (as well as the Two class if you intend to use in it an external main method for instance).
You can't access myplayer the way you do. Instantiate the First class, call makeContent (but unless you have a reason to have a null myplayer, why not do it directly in the constructor ?)
Your files must be named after your class name.
You have to import packages in order to access their content. As I don't know where the Playerclass comes from, I can only guess that you'll have to do the same for it (and check that this class too is public).
Make your instance variables private and provide accessors to them.
Here's a simple example:
First.java
package a;
public class First {
private Player myplayer;
public void makeContent() {
myplayer = new Player();
}
public Player getPlayer(){
return myplayer;
}
// optional, depends if you want to be able to change the player
public void setPlayer(Player p){
myplayer = p;
}
}
Two.java
package b;
import a.*;
public class Two {
public void useContent() {
First f = new First();
f.makeContent();
Player p = f.getPlayer();
p.play();
}
}
And the same goes on to instantiate and use the Two class...
Here's my problem in detail.
Setup:
I have class A that has a private member variable of class B.
A method(method1) in class A calls a non-static method(method2)
in class B.
Class B actually inherits method2 from a protected abstract class C and does not override it.
Problem:
I'm writing a test for class A.
In the test I'm mocking the call to method2.
Sample Code:
B b = Mockito.mock(B.class);
A a = new A(b);
Mockito.when(b.method2()).thenReturn(MY_LIST);
Now when I call method1(which in turn calls method2), I get a
NullPointerException.
Sample Code:
a.method1();
I'm assuming that this call is completely independent of the implementation of method2 since I'm mocking it. Is that wrong ? If not, what am I doing wrong ?
PS: class C is protected and Class A is in a different package from class B and C.
I see that you are using Mockito in your test. I have recently used it on a project and I did a test project with the following.
First a service (A) with uses another class (B).
public class Service {
private NonStaticClass nonStatic;
public NonStaticClass getNonStatic() {
return nonStatic;
}
public void setNonStatic(NonStaticClass nonStatic) {
this.nonStatic = nonStatic;
}
public int useStaticService () {
return 2*StaticClass.staticMethod();
}
public Integer getLastUse () {
return this.nonStatic.getLastUse();
}
}
Then here is the (B) class:
public class NonStaticClass {
private Integer lastUse = new Integer(0);
public Integer getLastUse() {
return lastUse++;
}
}
In order to test everithing is working i created a test for it.
import static org.mockito.Mockito.when;
import junit.framework.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
#RunWith(MockitoJUnitRunner.class)
public class TestNonStaticMock {
private final Integer staticMethodOutput = 10;
#Mock
NonStaticClass mock = new NonStaticClass();
#InjectMocks
Service service = new Service();
#Before
public void before () {
setMock();
}
private void setMock() {
when(mock.getLastUse()).thenReturn(staticMethodOutput);
}
#Test
public void mockNonStaticMethod () {
Integer result = service.getLastUse();
System.out.println(result.toString());
Assert.assertEquals(staticMethodOutput, result);
}
}
Hope it can be usefull.
This question already has answers here:
nested type cannot hide an enclosing type
(5 answers)
Closed 7 years ago.
I have a problem with my java plugin.
This is never i had before. Normal it went all good.
Here is the code:
package me.brian.CubeRanks;
import me.brian.CubeRanks.CubeRanks;
import org.bukkit.plugin.java.JavaPlugin;
import java.util.ArrayList;
import java.util.logging.Logger;
import org.bukkit.ChatColor;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
public class CubeRanks {
public class CubeRanks extends JavaPlugin {
public static CubeRanks plugin;
public final Logger log = Logger.getLogger("Minecraft");
public boolean enabled = false;
public final PlayerListener pl = new PlayerListener(this);
public final ArrayList<Player> OreHunterUsers = new ArrayList<Player>();
public String cr = "[CubeRanks] ";
#Override
public void onEnable() {
log.info(cr + "is now enabled.");
PluginManager pm = getServer().getPluginManager();
pm.registerEvents(pl, this);
}
#Override
public void onDisable() {
log.info(cr + "is now disabled.");
}
#Override
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
if(commandLabel.equalsIgnoreCase("CubeRanks")) {
if(args.length==0) {
if(!enabled) {
enabled = true;
((Player) sender).sendMessage(ChatColor.GREEN + cr + "is now enabled");
}
else {
enabled = false;
((Player) sender).sendMessage(ChatColor.RED + cr + "is now disbaled");
}
}
}
return false;
}
}
}
Can someone see whats wrong?
Whole Error Code:
Description Resource Path Location Type
The nested type CubeRanks cannot hide an enclosing type CubeRanks.java /CubeRanks/src/me/brian/CubeRanks line 16 Java Problem
Photo of the error: This is the error
You have a class enclosing a class of the same name. I'm not sure why you have that outer CubeRanks class, but either get rid of the outer one, or rename one of them.
public class CubeRanks { // remove or rename
public class CubeRanks extends JavaPlugin { // or rename this
...
}
}
Syntactically, the reason why you can't have an inner class that is the same name as its outer class is to remove the following ambiguity:
public class Ambiguous {
public class Ambiguous {
Ambiguous a; // <-- which type is Ambiguous referring to here?
}
}
You could argue that the most recent enclosing type name could take precedence, but then how would you refer to the outer type? While there are theoretically ways to design a language around all of that, it's easier to just disallow it.