Java reflection mechanism implementation method:
Reflection setting attributes:
A A = new ();
Field field = A. getclass (). getdeclaredfield ("X ");
Field. setaccessible (true );
Field. Set (A, 1 );
Reflection read attributes:
Field F = A. getclass (). getdeclaredfield ("X ");
F. setaccessible (true );
System. Out. println (F. Get ());
References are very interesting.
Box A = new box ();
Box B = new box ();
At this time, a and B correspond to their respective memory addresses. If
A = B;
At this time, both A and B point to the same memory address, that is, B's memory address. That is to say, now you change the value of member variable B in;
If the value of the member variable in B is changed, the corresponding variable of a also changes. The reference is just a memory address.
[Object? Reference? I cannot tell you clearly !]
A A = new ();
Generates an object of type A. A is a reference of this object, that is, a points to the real object in heap,
A and other basic data types are stored in the stack. That is, the object is controlled by reference,
At the underlying layer, A is more like a pointer.
A is the object, and there is no big problem for beginners, because the operation on a is the operation on the object pointed to by.
The problem is that when the direction of a changes, a is the object and cannot adapt to the needs of program design. Let's look at a simple program:
Class
{
Private int I = 0;
Public void SETI (int x)
{
I = X;
}
Public int Geti ()
{
Return I;
}
}
Public class myref1
{
Public static void main (string [] ARGs)
{
A A = new ();
A B = new ();
A. SETI (10 );
B. SETI (15 );
System. Out. println ("A's I =" + A. Geti ());
System. Out. println ("B I =" + B. Geti ());
A = B;
A. SETI (20 );
System. Out. println ("A's I =" + A. Geti ());
System. Out. println ("B I =" + B. Geti ());
}
}
I = 10 of
I = 15 of B
I = 20 of
I = 20 of B
Therefore, A and B are references to objects. When we assign A to B, A has pointed to B again,
The operation directed to a after a change is the operation directed to B.
We know that Java defines constants through final:
Final int I = 10;
When we re-assign a value to a constant, a compilation error occurs:
I = 5; // compilation failed
We can also use final to define constant objects:
Final A = new ();
In this case, we cannot assign a value to.
If a is an object, the object cannot be changed. In fact, a is only a reference and can only point to the object originally pointed,
It does not mean that the State of the object it refers to cannot be changed. Therefore, we can change the state of the object without changing the original direction of.
In short, Java uses renfence to manipulate objects.
Parameter [value transfer? Reference transfer? I cannot tell you clearly !]
Public class myref2
{
Static int x = 10;
Static int y = 20;
Public static void fangfa (int I)
{
I ++;
X = I;
}
Public static void main (string [] ARGs)
{
System. Out. println ("x =" + x );
System. Out. println ("Y =" + y );
Myref2.fangfa (y );
System. Out. println ("x =" + x );
System. Out. println ("Y =" + y );
}
}
Obviously:
X = 10
Y = 20
X = 21
Y = 20
The value of Y has not changed. myref2.fangfa (y) only uses the value of Y, and the I ++ in it does not affect y itself.
Obviously, the Java parameter is a value transfer, but why is there a reference transfer statement? Take a look at the following program:
Class
{
Private int I = 0;
Public void SETI (int x)
{
I = X;
}
Public int Geti ()
Return I;
}
}
Public class myref1
{
Public static void seta1 (A newa, int T)
{
Newa. SETI (t );
}
Public static void main (string [] ARGs)
{
A A = new ();
System. Out. println (A. Geti ());
Myref1.seta1 (A, 30 );
System. Out. println (A. Geti ());
}
}
According to the value transfer statement, myref1.seta1 (A, 30); will use a replica of the object referred to by,
In the end, it does not work for this object, but the fact is that the method has a function for this object, and the program will display 0, 30.
So, is the Java parameter a value passing error? Actually not,
We should remember that A is only the reference of an object, and the review of the reference and the original reference point to the same object,
The restore operation is the same as the operation on a, and finally the operation directed to the object. Therefore, Java parameters only have the value to pass.