Access public enum from inner class - java

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

Related

'me.kczor.managers.UHCManager' is not an enclosing class

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

Get Inner Class Instance status

Use case is something similar to below code. There is a class(Inner_Demo) inside another class(Outer_Demo). Inner_class will be instantiated upon some condition in the outer class private method.
class Outer_Demo {
public Outer_Demo() {
test();
}
// private method of the outer class
private void test() {
Inner_Demo demo;
if(condition)
demo = new Inner_Demo();
}
// inner class
class Inner_Demo {
}
}
main(){
Outer_Demo outer = new Outer_Demo();
// Here I need to check is Inner class got instantiated
// Trying to print the value as below leads to error create
// field/constant Inner_Demo in Outer_Demo
System.out.println(Outer_Demo.Inner_Demo); // outer.Inner_Demo
/* Storing the created instance to Outer_Demo.Inner_Demo
is allowed */
Outer_Demo.Inner_Demo inst = outer.new Inner_Demo();
System.out.println(inst);
}
I need to test, Is inner class is Instantiated or not. I got to know that calling the inner class in above way is incorrect.
Reflection might have used if the field demo in the Outer_Demo class's method test is not local/ have class level access.
Can anybody help me to understand, Is there any way find inner class status. Any links to subject is helpful. Thanks.
You probably want to check if an object of that class has been instantiated.
For this task you should declare an Inner_Demo field in your Outer_Demo class:
class Outer_Demo {
public Outer_Demo() {
test();
}
Inner_Demo innerDemo;
...
Now, each time the object is instantiated, this field must be assigned a value:
innerDemo = new Inner_Demo();
And finally, when you want to check if the object exists, you just do it like:
if (innerDemo == null) {
//object does not exist yet and has to be instantiated
} else {
//object does exist and can be used
}

Access private inner class, through a public outer class - from another class

I am completely stuck on this and have been playing around with it for a while. I have a class "launcher", from which I want to access an instance of a private inner class "PropertyInstance", through the outer class "PropertyManager".
So, in my launcher I would like to write:
PropertyManager pm = new PropertyManager();
PropertyInstance pi = pm.getInstance("brickbreaker.properties");
In my PropertyManager class I have written the following code:
public PropertyInstance getInstance(String location)
{
PropertyInstance pi = null;
if(!propertyList.contains(location))
{
System.out.println("it does not contain it yet, so we will create it");
pi = new PropertyInstance(location);
propertyList.add(pi);
}
return pi;
}
And inside this class, I have the following inner class:
private class PropertyInstance
{
}
Which is irrelevant apart from the private modifier.
The problem is that I can not access the PropertyInstance class from my Launcher due to it being private, and I do not seem able to find a workaround so really any help is appreciated. If it can even be done.
EDIT: It seems to be unclear that I'm looking for a workaround whilst keeping the inner class private, sorry for the confusion! :)
Make an interface that defines the public access you want to your private class, make your private class implement it, and return it as that.
The workaround is to make it public.

jMockit's access to a private class

I have a public class with a private class inside it:
public class Out
{
private class In
{
public String afterLogic;
public In(String parameter)
{
this.afterLogic = parameter+"!";
}
}
}
And wanted to test the In class with jMockit. Something along these lines:
#Test
public void OutInTest()
{
Out outer = new Out();
Object ob = Deencapsulation.newInnerInstance("In", outer); //LINE X
}
The problema is, in LINE X, when trying to cast ob to In, the In class is not recognized.
Any idea how to solve this?
Thanks!
The only constructor in class In takes a String argument. Therefore, you need to pass the argument value:
Object ob = Deencapsulation.newInnerInstance("In", outer, "test");
As suggested in the comment one way is to change the access modifier of the inner class from private to public.
Second way (in case you don't want to make your inner class public), you can test the public method of outer class which is actually calling the inner class methods.
Change the scope of the inner class to default then make sure that the test is in the same package.
There are two approaches, first as mentioned in other posts to change the scope to public. The second which I support is, to avoid testing private class altogether. Since the tests should be written against testable code or methods of the class and not against default behavior.

Java: reusable encapsulation with interface, abstract class or inner classes?

I try to encapsulate. Exeption from interface, static inner class working, non-static inner class not working, cannot understand terminology: nested classes, inner classes, nested interfaces, interface-abstract-class -- sounds too Repetitive!
BAD! --- Exception 'illegal type' from interface apparently because values being constants(?!)
static interface userInfo
{
File startingFile=new File(".");
String startingPath="dummy";
try{
startingPath=startingFile.getCanonicalPath();
}catch(Exception e){e.printStackTrace();}
}
MANY WAYS TO DO IT: Interface, static inner class image VS non-static innner class image
import java.io.*;
import java.util.*;
public class listTest{
public interface hello{String word="hello word from Interface!";}
public static class staticTest{
staticTest(){}
private String hejo="hello hallo from Static class with image";
public void printHallooo(){System.out.println(hejo);}
}
public class nonStatic{
nonStatic(){}
public void printNonStatic(){System.out.println("Inside non-static class with an image!");}
}
public static class staticMethodtest{
private static String test="if you see mee, you printed static-class-static-field!";
}
public static void main(String[] args){
//INTERFACE TEST
System.out.println(hello.word);
//INNNER CLASS STATIC TEST
staticTest h=new staticTest();
h.printHallooo();
//INNER CLASS NON-STATIC TEST
nonStatic ns=(new listTest()).new nonStatic();
ns.printNonStatic();
//INNER CLASS STATIC-CLASS STATIC FIELD TEST
System.out.println(staticMethodtest.test);
}
}
OUTPUT
hello word from Interface!
hello hallo from Static class with image
Inside non-static class with an image!
if you see mee, you printed static-class-static-field!
Related
Nesting classes
inner classes?
interfacses
The problem is that you're writing code outside of a method. You do need a class for this and you must put your code inside a method. For example:
static class UserInfo
{
public static void myMethod()
{
File startingFile = new File(".");
String startingPath = "dummy";
try
{
startingPath = startingFile.getCanonicalPath();
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
This does assume that java.io.File was imported.
You can then call UserInfo.myMethod();
You might also want to import java.util.IOException and catch an IOException instead of a general Exception.
Also, classes and interfaces start with a capital letter by Java conventions.
EDIT: To describe your recent comment on your question:
Use an interface when you want to force similar classes (Think different types of DVD players) to have the same basic functionality (playing dvds, stopping, pausing. You use an abstract class similarly, but when all of the classes will implement some of the same things the same way.
I think you wanted to do this:
static class userInfo
{
public static void something() {
File startingFile=new File(".");
String startingPath="dummy";
try{
startingPath=startingFile.getCanonicalPath();
}catch(Exception e){e.printStackTrace();}
}
}
you cant put code in an interface, an interface only describes how an object will behave. Even when you use Classes, you should put this kind of code in a method, and not directly in the class body.
You can't have actual code in an interface, only method signatures and constants. What are you trying to do?
Looks like you want to write a class here.
You cannot have code in interfaces. Just method signatures.
Top level interfaces cannot be static.
I suggest you start your learning of Java here.

Categories