The getClass() method, the Class object, and an introduction to Reflection - 1

"Hi, Amigo!"

"Now it's time for the main event. We'll get acquainted with the Class class and touch on Reflection.
As you probably already realized, everything in Java is an object. And what does an object need? What does each object have that defines what it's all about?"

"A class!"

"Right! Well done. Each object has a class. But getting back to objects... Some objects entirely contain an entity, while others simply help manage it."

"This latter type includes FileOutputStream and Thread. When you create a Thread object, a new thread is not created. The thread is created by the Java virtual machine after the start() method is called. This object simply helps manage the process."

"Same with FileOutputStream: the file is stored on disk, and the OS manages storage and access. But we can interact with it through File objects, once again with the help of the Java virtual machine."

"Yes, I already understand that."

"So, there's a special class called Class for interacting with classes."

"That wasn't hard to guess."

"Yep. Each time the Java virtual machine loads a new class into memory, it creates a Class object, which you can use to get specific information about the loaded class."

"Each class and object is associated with a «Class object»."

Example Description
Class clazz = Integer.class;
Gets the Integer class's Class object.
Class clazz = int.class;
Gets the int class's Class object.
Class clazz = "123".getClass();
Gets a String object's Class object.
Class clazz = new Object().getClass();
Gets the Object object's Class object.

"Wow! How interesting!"

"Do you remember that the word class is a keyword in Java and can't be used as a variable name?"

"Oh yeah, I know, I know. I just forgot."

"Have you already used the Class object somewhere?"

"Yes, we used it when we wrote our own implementation of the equals method."

"Yes, you can use the getClass() method to test whether objects have the same class."

"And what can you do with this object?"

"Well, a lot of things:"

Java code Description
Class s = int.class;
String name = s.getName();
Gets the class name.
Class s = Class.forName("java.lang.String");
Gets a class by name.
Object o1 = String.valueOf(1);
Object o2 = 123 + "T";
o1.getClass() == o2.getClass();
Compares objects' classes.

"Interesting, but not as cool as I thought."

"You want it to be cool? There's also ReflectionReflection is super cool."

"What's Reflection?"

"Reflection is a class's ability to obtain information about itself. Java has special classes: Field and Method, which are similar to the Class class for classes. Just as Class objects let you get information about a class, Field objects provide information about a field, and the Method object provides information about a method. And look at what you can do with them:"

Java code Description
Class[] interfaces = List.class.getInterfaces();
Gets a list of Class objects for the List class's interfaces
Class parent = String.class.getSuperclass();
Gets the Class object of the String class's parent class
Method[] methods = List.class.getMethods();
Gets a list of the List class's methods
String s = String.class.newInstance();
Creates a new String
String s = String.class.newInstance();
Method m = String.class.getMethod("length");
int length = (int) m.invoke(s)
Gets the String class's length method and calls it on the String s

"Wow! Now that's really cool!"

undefined
12
Task
Java Core, level 9, lesson 5
Locked
Even characters
Read 2 file names from the console. Output to the second file all characters from the first file with an even ordinal number (ordinal numbers start with 1). Example first file: text in file Output in the second file: eti ie Close the IO streams.
undefined
20
Task
Java Core, level 9, lesson 5
Locked
Counting words
Read a file name from the console. The file contains words separated by punctuation marks. Output to the console the number of times the word "world" appears in the file. Close the streams.
undefined
20
Task
Java Core, level 9, lesson 5
Locked
Picking out numbers
Read 2 file names from the console. Output to the second file all of the numbers from the first file. Separate the numbers with a space. Close the streams. Example file content: 12 text var2 14 8v 1 Result: 12 14 1
undefined
12
Task
Java Core, level 9, lesson 5
Locked
Changing punctuation marks
Read 2 file names from the console. The first file contains text. Read the contents of the first file and change all the periods (".") to exclamation points ("!"). Then output the result to the second file. Close the streams.
undefined
12
Task
Java Core, level 9, lesson 5
Locked
Punctuation
Read 2 file names from the console. The first file contains text. Read the contents of the first file, and remove all punctuation marks, including newline characters. Then output the result to the second file. Close the streams.