Why class name is same as class used in import statement - java

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 {

Related

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";
}
}
}
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.

How to make Intellij detect Spring beans defined in generic parent class

I have a lot of code looking like this
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
#Configuration
public class ConfigurationBase<T>
{
public #Bean(name="S1") T getS1() { return null; }
public #Bean(name="S2") T getS2() { return null; }
}
And a derived class looking like this:
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
public class ConfigurationDerived extends ConfigurationBase<String>
{
#Override
public String getS1() { return "S1+++"; }
public #Bean(name="S3") String getS3() { return "S3"; }
public #Bean(name="ID") String getCompositeName(
#Qualifier("S1") String s1,
#Qualifier("S2") String s2,
#Qualifier("S3") String s3)
{
return "hello!";
}
}
When the base class is not generic, IntelliJ is able to find all the Spring dependencies.
But when as in this example, the base class is generic, it can't find the beans defined in the base class
For example here, it won't find the beans "S1" and "S2"
Is there a work around?

Java is class abstract?

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.

Importing Map Keys By Interface

Let's say I have a class with a hashmap data structure:
import java.util.HashMap;
import java.util.Map;
import org.openqa.selenium.By;
public abstract class Data {
protected Map<String, By> identifiers = new HashMap<>();
public By getSelector(String key) {
return identifiers.get(key);
}
}
3 subclasses which inherit it:
public class DataSpecific1 extends Data {
}
public class DataSpecific2 extends Data implements KeysForDataSpecific23 {
}
public class DataSpecific3 extends Data implements KeysForDataSpecific23 {
}
Now I want to define an interface which adds hashmap keys to only 2 of these subclasses:
import org.openqa.selenium.By;
public interface KeysForDataSpecific23 {
identifiers.put("key", By.cssSelector("#something"));
}
Obviously, this will cause a compile error since the interface can not inherit from the Data class and the variable identifiers is not defined.
Any way of doing it without redundancy such as adding the put() line in both affected classes?
Nowadays, You have facility in java 8 to have default method in interface ,
With this you can do as follows:
public interface KeysForDataSpecific23 {
default void setKeys(){
identifiers.put("key", By.cssSelector("something"));
}
}
default methods can give inception behavior to interfaces.
You can't, using interfaces.
You can do this, though:
public abstract class KeysForDataSpecific23 extends Data {
protected KeysForDataSpecific23() {
identifiers.put("key", By.cssSelector("something"));
}
}
public class DataSpecific2 extends KeysForDataSpecific23 {
}
public class DataSpecific3 extends KeysForDataSpecific23 {
}

Type The nested type CubeRanks cannot hide an enclosing type [duplicate]

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.

Categories