I have problem with class Main.I tried import final boolean from UHCManager.java to Main.java , and get message "'me.kczor.managers.UHCManager' is not an enclosing class"
Class Main:
import me.kczor.managers.UHCManager;
public class Main extends JavaPlugin {
public void onEnable(){
UHCManager.this.statusGame = false;
}
}
Class UHCManager:
package me.kczor.managers;
public class UHCManager implements Listener, CommandExecutor {
public final boolean statusGame = false;
When you write Class.this.xxx <-- You are requesting the enclosing class. That is why the error you are receiving is
[['me.kczor.managers.UHCManager' is not an enclosing class]]
When you write this.xxx <-- You are requesting (this) class, where you are in.
In order to access a variable from a different class, whether it is private/public/protected. Create a new instance of the class you want, and create getters/setters for all the fields you want to access/modify from other classes.
Very simple example of how to use Class.this.
What is the difference between Class.this and this in Java
Related
i have this code :
package com.example.android.cars.data;
public final class DataBaseContract {
public static final class Table1Entry implements BaseColumns {
/** Name of database table for cars */
public final static String TABLE_NAME = "car";
}
}
i use Table1Entry in another class with different package and i import the nested class like this
import com.example.android.cars.data.DataBaseContract.Table1Entry;
this allow me to use nested class without outer prefix DataBaseContract,
my question is when i removed static from nested class the code still work, how can this accrue in this case !! i need outer instance to access it!!
Yes you would need an instance of the outerclass IF you wanted to access instance methods of the inner class. However from your example you are only accessing static fields, therefore because the field is static you can access it directly like you explained.
I have two classes, Main and SPBHomeHelp. Here is the code for Main:
public class Main{
SPBHomeHelp homeHelp;
public Main(){
homeHelp = new SPBHomeHelp();
Home home = new Home();
}
}
Here is the code for SPBHomeHelp:
public class SPBHomeHelp{
public SPBHomeHelp(){
}
public class Home{
public Home(){
System.out.println("Entered Home Constructor");
}
}
}
Main and SPBHomeHelp are two different .java files. I can easily declare and instantiate an instance for SPBHomeHelp. But I want to have an instance of Home, which is a class nested inside SPBHomeHelp, in Main too. I tried:
Home home = new Home();
because Home is a public, but that doesn't work. How can I create an instance of Home in Main?
There are two ways to implement this:
First,, just like the answer you accepted:
SPBHomeHelp.Home home = new SPBHomeHelp().new Home();
Second, make your Home static, then, in your Main.main() method,
SPBHomeHelp.Home home = new SPBHomeHelp.Home();
will be OK.
PS: what is the difference between static inner class and non-static inner class is: in a non-static inner class, there is a this which refer to the outer class, which means you can use fields and methods of the outer class in a non-static inner class. But you can't use fields and methods of the outer class in a static inner class except they are also static.
home is an instance of the inner-class Home type (from the class SPBHomeHelp). You need an instance of that class (that ishomeHelp), in order to construct a Home instance. I think you're looking for something like
SPBHomeHelp.Home home = homeHelp.new Home();
Change the class Home to static, and static import your Home class.
public class SPBHomeHelp{
public SPBHomeHelp(){
}
public static class Home{
public Home(){
System.out.println("Entered Home Constructor");
}
}
}
Main:
static import SPBHomeHelp.Home;
public class Main{
SPBHomeHelp homeHelp;
public Main(){
homeHelp = new SPBHomeHelp();
Home home = new Home();
}
}
I have an enum defined in a file State.java:
public enum State {
NONE, CHOOSINGMUD, PLAYING
}
I have a public class Server from which I can access the enum with State.NONE and this works fine.
However, within Server I have a public inner class defined, and when I try to acces State.NONE there, I get a cannot find symbol error:
symbol: variable State
location: class Server
How do I use State from within this inner class? Is it possible to do so directly like I do inside my Server class? It seems to me like it's looking for State inside it's parent class (Server).
Here is the code for my inner class. I've simplified it, but the print statement here will return the same error:
public class ClientConnection extends Thread
{
private PlayerInterface player;
ClientConnection(PlayerInterface player) throws IOException
{
this.player = player;
System.out.println(State.NONE);
}
}
Your State class is conflicting with inner class java.lang.Thread.State which does not have a NONE field. Rename your enum class to something else and your code will work
I just created a file with this in it
public enum State {
on, off
}
then a file with this in it
public class SomeClass {
private static class SomeInnerClass{
State s = State.off;
public void doSomething(State state){
if(state == State.off){
//do something
}
}
}
}
seems to work fine. I can access it at the inner class's class level and in a method. Hope looking at this helps
I wrote the getPlugin() method to be able to get the main class from another class.
public class Main extends JavaPlugin {
public Main getPlugin() {
return this;
}
}
But when I try to call it...
public class Arena {
private Main plugin = Main.getPlugin();
}
...Eclipse gives me the following error:
Cannot make a static reference to the non-static method getPlugin() from the type Main
I have used static, but static gives me issue's in a lot of different places, and I've seen that static is usually a bad way of doing stuff. Causes memory leaks and stuff.
I have tried using getters and setters, but those need to be static too?
The code I've been using is very messy and I'd like to find a cleaner way of accessing another class.
If you want to avoid using static methods, you need to pass variables as a parameter to the constructor of objects. In your example, it would work like this:
public class Arena {
private final Main plugin;
public Arena(Plugin plugin) {
this.plugin = plugin;
}
}
And then you can create an Arena from your main plugin class, and pass in this as a parameter:
public class Main extends JavaPlugin {
#Override
public void onEnable() {
Arena arena = new Arena(this);
}
}
This problem occurs because there already is a getPlugin() method in the JavaPlugin superclass, so when you do Main.getPlugin() you are trying to call the non-static one.
Also, your own method is non-static.
Here is how I do.
You have to use a different name and make it static. Also, you should initialize its value on onEnable().
public final class Example extends JavaPlugin {
private static Plugin main;
public static Plugin getMain() {
return main;
}
#Override
public void onEnable() {
main = this;
}
}
I've recently (4 days ago) started programming in JAVA. I have some overall programming experience from C++ and PHP. My question is: can we implement a function in JAVA, that is available in all classes? I'm thinking of some global logging function, that I need to call in several places (log events, errors, etc.).
Imagine I have two classes, A and B. I need to call logging function in both of them, but I don't want to copy whole function body (awful thing I believe), and I want to call it strict (without creating another class, instantiating it, and then calling from the instance), like logEvent(someVariable). So I should use an abstract class C, which A and B will extend, BUT they are already an extension of other class (built-in). Since multiple inheritance isn't allowed (is it?), I need to do some trick. Singleton is not pleasing me too. In PHP or C++ I would just create separate file with function body and then include it.
Here is how I want to use it:
public class A extends SomeClass {
String error = "Error from class A";
logEvent(error);
}
public class B extends SomeOtherClass {
String error = "Error from class B";
logEvent(error);
}
Put a static method in any class (it could be a utils class, or whatever), then call it like this: ClassName.functionName()
Static methods belong to the class, not instances of the class, so you don't need to instantiate the class to access the method
But everything in Java has to be in a class, so you can't access it without the class name.
You should use static method:
package xxx;
public class Util{
public static void logEvent(String error){
...
}
}
and import static:
import static xxx.Util.*;
public class A extends SomeClass {
String error = "Error from class A";
logEvent(error);
}
You may use static method.
Define a class with a static method:
public class Util{
public static void logEvent(String error){
...
}
}
Then, you can use static metod like this way:
public class A extends SomeClass {
String error = "Error from class A";
Util.logEvent(error);
}
you may take a look here to learn more about static method, http://www.leepoint.net/notes-java/flow/methods/50static-methods.html