{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

presentation30 - public static void main(String args Class...

Info icon This preview shows pages 1–20. Sign up to view the full content.

View Full Document Right Arrow Icon
Reflection Soheil Hassas Yeganeh Computer Engineering Department Sharif University of Technology
Image of page 1

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
A Problem 2
Image of page 2
A Problem We have an unknown set of List implementations . We can use any of them in our program. But, we want the user to specify his/her wanted implementation. 2
Image of page 3

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
What we need ? (1) 3 Method Class Name Class
Image of page 4
What we need ? (1) 1. A mechanism to load the class using a string . I want to load a class named “java.util.ArrayList”. 3 Method Class Name Class
Image of page 5

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
What we need ? (2) 4 Method Class Object
Image of page 6
What we need ? (2) 1. A mechanism to instantiate a class at runtime. I want to instantiate a Class object java.util.ArrayList . 4 Method Class Object
Image of page 7

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
java.lang.Class 5 Test.java class Test { public static void main(String[] args){ Class klass = Test.class; System.out. println(klass); } }
Image of page 8
java.lang.Class Class is the class for classes in Java! If you want to access the Class object of a class , you can use .class field. What are the methods of the Class? 5 Test.java
Image of page 9

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 10
Image of page 11

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 12
Image of page 13

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 14
Image of page 15

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 16
Image of page 17

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 18
Image of page 19

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 20
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: public static void main(String[] args){ Class klass = Test.class; System.out. println(klass); } } Class Methods 6 Class Methods • Class should have methods that allow us to access its • Fields , Methods , Super Class , Interfaces , Package , Visibility , . .. • Class.forName() gets a string and loads a class. • Let’s see a sample! 6 Other Classes 7 Other Classes • java.lang.refect. Constructor • java.lang.refect. Field • java.lang.refect. Method • java.lang.refect. Package • .... 7 Class of Object 8 Class of Object • Object’s getClass() method! 8 Accessibility 9 Accessibility • A feld, a constructor, or a method, can be private , and we do not have access to it . • Using reFection we can make it accessible . • Let’s see a sample! 9 The Problem 10 The Problem • Let’s write a program to solve our problem. 10 Any Questions?...
View Full Document

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern