) Difference between field getfield (string name) and field getdeclaredfield (string name)

Source: Internet
Author: User

Field getfield (string name) returns the Field object of all public member variables declared by the loaded class, including the member variables inherited from the parent class. The parameter name specifies the name of the member variable.

Field getdeclaredfield (string name) cannot obtain the member variables inherited from the parent class.

**************************************** *******

 1   Package  Cn.com. VO;
2
3 Public Class Father {
4 Public String name = " Father " ;
5
6 Public String sex = " Male " ;
7
8 Public String getname (){
9 Return Name;
10 }
11
12 Public Void Setname (string name ){
13 This . Name = Name;
14 }
15
16 Public String getsex (){
17 Return Sex;
18 }
19
20 Public Void Setsex (string sex ){
21 This . Sex = Sex;
22 }
23
24
25 }
26 ++
27
28 Package Cn.com. VO;
29
30 Public Class Son Extends Father {
31
32 // Public string name;
33 Public Int Age = 20 ;
34
35
36 }
37 ========================================================== =
38
39 Package Cn.com. VO;
40
41 Import Java. Lang. Reflect. constructor;
42 Import Java. Lang. Reflect. field;
43 Import Java. Lang. Reflect. invocationtargetexception;
44 Import Java. Lang. Reflect. method;
45
46 Import Cn.com. VO. uservo;
47
48 Public Class Testreflect {
49
50 /**
51 * Java reflection mechanism
52 */
53 Public Static Void Main (string [] ARGs ){
54 Try {
55 // Dynamic Loading of Classes
56 Class C = Class. forname ( " Cn.com. VO. Son " ); // Method 1
57 Son Vo = (Son) C. newinstance ();
58
59 Field F = C. getfield ( " Name " );
60 System. Out. println (F. Get (VO ));
61
62
63 // Note: lower partCodeAn error is reported,
64 /* Field F2 = C. getdeclaredfield ("name ");
65 System. Out. println (f2.get (VO )); */
66
67 } Catch (Classnotfoundexception e ){
68 // Todo auto-generated Catch Block
69 E. printstacktrace ();
70 } Catch (Securityexception e ){
71 // Todo auto-generated Catch Block
72 E. printstacktrace ();
73 } Catch (Nosuchfieldexception e ){
74 // Todo auto-generated Catch Block
75 E. printstacktrace ();
76 } Catch (Instantiationexception e ){
77 // Todo auto-generated Catch Block
78 E. printstacktrace ();
79 } Catch (Illegalaccessexception e ){
80 // Todo auto-generated Catch Block
81 E. printstacktrace ();
82 }
83 }
84
85 }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.