I have learnt that static nested class should be accessed like a field of outer class(line 2). But even instantiating the inner class directly worked (line 1). Can you please help me understand?
public class OuterClass
{
public OuterClass()
{
Report rp = new Report(); // line 1
OuterClass.Report rp1 = new OuterClass.Report(); // line 2
}
protected static class Report()
{
public Report(){}
}
}
accessed like a field of outer class
And that's what you are doing. Imagine this:
class OuterClass
{
SomeType somefield;
static SomeType staticField;
public OuterClass()
{
//works just fine.
somefield = new SomeType();
//also works. I recommend using this
this.somefield = new SomeType();
//the same goes for static members
//the "OuterClass." in this case serves the same purpose as "this." only in a static context
staticField = new SomeType();
OuterClass.staticField = new SomeType()
}
}
Related
New to Java. I'm trying to create a class to convert to JSON string to send as POST request using GSON. This class was created within a public class Called BertClient:
private class BertJsonRequest {
private Integer id;
private List<String> texts;
public BertJsonRequest(int x, String text) {
this.id = x;
this.texts = new ArrayList<>();
this.texts.add(text);
}
}
How I use that:
BertJsonRequest rawRequestBody = new BertJsonRequest(1, text);
Gson gsonToJson = new Gson();
String requestBody = gsonToJson.toJson(rawRequestBody);
For the line where I'm creating new BertJsonRequest My IDE tells me that BertClient.this cannot be referenced from a static content.
I wonder what that means.
Am I building the constructor correctly?
I think I'm not. I just want to be able to pass in a String so that constructor can create a List of String using that String.
Your class access modifier is set to private. Try setting the access modifier to public instead.
public class BertJsonRequest {
private Integer id;
private List<String> texts = new ArrayList<>();
public BertJsonRequest(int x, String text) {
id = x;
texts.add(text);
}
}
What I understood by reading your comments on other's answers was, that your BertClientRequest probably is an inner class.
In case it really is an inner class, and you try to call it in a static method of your containing class, it becomes apparent that you cannot instantiate your inner class as that inner class is not static.
public class BertClient {
private class BertClientRequest {
/* some code */
}
static void aStaticMethod() {
// ...
// Inner class BertClientRequest is unknown to your static method as it is not static,
// thus giving you a compile time error
BertClientRequest rawRequest = new BertClientRequest(1, text);
// ...
}
}
The fix would be in this case to change your inner class to static:
private static class BertClientRequest
I guess your BertJsonRequest is a inner class of BertClient. You can't instantiate BertJsonRequest outside of BertClient. You can make BertJsonRequest class static for this to work.
I am clear that accessing a private field in Java could be easily achieved by using Reflection. As is shown in posts as How to read the value of a private field from a different class in Java? and there are many.
To achieve that , the critical move is to set accessibility.
Field f = obj.getClass().getDeclaredField("aaa");
f.setAccessible(true);
But in my case, the situation is like:
class A{
private B b;
class B{
private String value;
}
}
and I want to get value of a.b.value in another class. When I was trying, I intended to do it as
A obj = createInstanceA();
Field f = obj.getClass().getDeclaredField("b");
f.setAccessible(true);
A.B b = f.get(obj);
Field f2 = b.getClass().getDeclaredField("value");
f2.setAccessible(true);
String value = f2.get(b);
Which doesn't work out because B could not be declared out of A.
Do I have other options if Class A can not be modified?
You have do like this,
public class A {
private B b = new B();
class B {
private String value = "String";
}
}
public class ClassB {
public static void main(String args[]) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException{
A obj = new A();
Field f = obj.getClass().getDeclaredField("b");
f.setAccessible(true);
A.B b = (B) f.get(obj);
Field f2 = b.getClass().getDeclaredField("value");
f2.setAccessible(true);
String value = (String) f2.get(b);
System.out.println(value);
}
}
What you are missing is to setAccessible(true) to inner class field.
As a first, in your example field b is null. Is this correct?
So, you try to get class of null.
As a second, in your example you use inner classes and there is a specific langugage mechanizm. You can create instance of class B only by some instance of class A. And all instances of class B has access to private field of it's parrent (class A). As in this example.
class OuterClass
{
// static member
static int outer_x = 10;
// instance(non-static) member
int outer_y = 20;
// private member
private int outer_private = 30;
// inner class
class InnerClass
{
void display()
{
// can access static member of outer class
System.out.println("outer_x = " + outer_x);
// can also access non-static member of outer class
System.out.println("outer_y = " + outer_y);
// can also access private member of outer class
System.out.println("outer_private = " + outer_private);
}
}
}
// Driver class
public class InnerClassDemo
{
public static void main(String[] args)
{
// accessing an inner class
OuterClass outerObject = new OuterClass();
OuterClass.InnerClass innerObject = outerObject.new InnerClass();
innerObject.display();
}
}
May be inner classes can solves your problem? (You can read abou it here https://www.geeksforgeeks.org/nested-classes-java/)
Then reflecsoin is not needed.
Lets assume I have class:
class A {
protected int x; // no public getter, setter
}
Now I want to extend class A, to overwrite 'x' variable. The answer would be:
public class SomeClass {
someMethod() {
A extendedClass = new MyExtension(5);
}
class MyExtension extends A {
public MyExtension(int x) {
super.x = x;
}
}
}
And my question: is there any possibility to do it without defining nested class separately? I mean something like this
someMethod() {
A extendedClass = new A() {
// here do something like super.x = 5;
}
}
I tried with Calling newly defined method from anonymous class but it won't let me instantiate A class. Also I dont want to use reflection.
I simpy dont want to define nested class just to overwrite one property. Origin of the problem is Spring-integration ImapMailReceiver, where I want to overwrite task scheduler. Like below:
final ImapMailReceiver imapMailReceiver = new ImapMailReceiver() {
super.setTaskScheduler(MYTASKSCHEDULERHERE);
}
Your new A() { ... } still is just defining a new class. Thus, you cannot simply put any statement between the curly brackets, only field, method and nested type declarations may go there, but constructors are not allowed. You may add an instance initializer instead:
A extendedClass = new A() {
{
x = 5;
}
}
Why to use nested or anonymous inner class?
We can do overriding and access the overrided variable as below.
public class SomeClass extends A{
int val;
void someMethod(int val) {
super.x = val;
}
public static void main(String[] args) {
SomeClass sc = new SomeClass();
System.out.println(sc.x); // output=10
sc.someMethod(20);
System.out.println(sc.x); // output=20
}
}
class A {
protected int x = 10; // no public getter, setter
}
public class OuterClass {
private static class InnerClass {
public int id = 0;
}
private static InnerClass[] innerArr;
}
Need to get innerArr value with reflection in Java.
ClassLoader loader = Thread.currentThread().getContextClassLoader();
Class<?> cla = loader.loadClass("OuterClass");
Field innerArrRef = cla.getDeclaredField("innerArr");
innerArrRef.setAccessible(true);
OuterClass.InnerClass[] innerArrValue = (OuterClass.InnerClass[])innerArrRef.get(cla);
Above code doesn't work for InnerClass is a private class.
Let's adjust your OuterClass code a bit to make it more interesting: put in instance of InnerClass in the array:
private static InnerClass[] innerArr = { new InnerClass() };
Now you can't directly refer to the private type InnerClass in another class that is trying to get the static field using reflection. You can first refer to it as an array of Object.
public static void main(String[] args) throws Exception {
Class<?> cla = OuterClass.class;
Field innerArrRef = cla.getDeclaredField("innerArr");
innerArrRef.setAccessible(true);
Object[] innerArrValue = (Object[]) innerArrRef.get(cla);
You'll need to use reflection and the Class object to use the InnerClas type.
One way is to use the declaredClasses property of the OuterClass' class object:
Class<?> inner = cla.getDeclaredClasses()[0];
But if there's more than one member class, you need to loop through the array and search for the right one. Another way is to use the knowledge that javac will give your InnerClass a fully-qualified type name that looks like mypackage.OuterClass$InnerClass:
Class<?> inner = cla.getClassLoader().loadClass(cla.getName() + "$InnerClass");
Once you have the class object of the InnerClass, you can use reflection to access its field and methods:
Field id = inner.getField("id");
System.out.println("Inner id: " + id.getInt(innerArrValue[0]));
}
So, here is the class with private Inner class declared inside and a private attribute.
I need to use Java reflection writing a test program in main function to execute this class.
public class Outter {
private Inner in;
public Outter(){
in = new Inner();
}
private class Inner{
private void test(){
System.out.println("test");
}
}
}
Here is test code:
my questions are listed following the statement.
public class Test
{
public static void main(String[] args) throws Exception
{
// 1. How do i create a Class type for Inner class since its modifier
// is private, if I am going to need .setAccessible() then how do i
// use it?
Class outter1 = Outter.class;
// 2. How do I pass parameters of type Inner to the Class object?
Constructor con = outter1.getConstructor(new Class[]{int.class});
// 3. Like this?
Field fields = outter1.getField("test");
fields.setAccessible(true);
// 4. Well I am lost what is the logic route for me to follow when
// using java reflection to execute a class like this!
Object temp = outter1.newInstance();
Outter outter = (Outter)temp;
System.out.println(fields.get(outter));
}
}
Here's a self-contained example of what you're trying to do.
Code you're running
try {
// gets the "in" field
Field f = Outer.class.getDeclaredField("in");
// sets it accessible as it's private
f.setAccessible(true);
// gets an instance of the Inner class by getting the instance of the
// "in" field from an instance of the Outer class - we know "in" is
// initialized in the no-args constructor
Object o = Object o = f.get(Outer.class.newInstance());
// gets the "execute" method
Method m = o.getClass().getDeclaredMethod("test", (Class<?>[])null);
// sets it accessible to this context
m.setAccessible(true);
// invokes the method
m.invoke(o, (Object[])null);
}
// TODO better handling
catch (Throwable t) {
t.printStackTrace();
}
Classes (inner/outer)...
public class Outer {
private Inner in;
public Outer() {
in = new Inner();
}
private class Inner {
private void test() {
System.out.println("test");
}
}
}
Output
test