Dowemo
0 0 0 0

A class object in java

Well, java everything is all objects. But if you often pay attention to business development, rather than a lot of frameworks such as spring, hibernate, and other source code, we usually see. Now, let's take a look at this mysterious class object.

What's the class object.

Each class corresponds to one class object. Although we don't normally operate this object directly, this object is very common, and the class file we compiled with our java file, which is the class object.
We define several classes, and we can see the compiled directory ( usually in the target directory ) under several class files.
1. For example, we define an employee class, so we see a employee file.

package com.sky.javase;publicclass Employee {
}

2. For example, we define a public book class, and a bookmark class, with two class files: Book.class, bookmark. Class.

package com.sky.javase;publicclass Book {
}
class BookMark{
}

3. For example, we define a person class and an internal class head, and you also get two class files: Person.class head. Class ( note that the naming rules for inner classes ) are.

package com.sky.javase;publicclass Person {
 class Head{
 }
}

In short, a pit, a pit, defines several classes, with several class files defined. These class files, saved, are the class objects we define classes. When the class object of this class is loaded into memory, it's used to create all of the objects of this class. ,

Three, how to get class objects?

According to the actual situation, we can get a variety of ways.
Before the example begins, we define a class student that writes a static block of code and a block of code blocks. A class is loaded to invoke a static block of code that will invoke the constructor block. We see the difference in three ways through the output.

class Student{
 static{
 System.out.println("这是静态代码块!");
 }
 {
 System.out.println("这是构造代码块!");
 }
}

1. If you've just one I & tance object, call it directly ( inherited into object ):

publicclass ReflectTest {
 @Test
 publicvoidreflect_getClass(){
 Student stu = new Student();
 Class<?> sClass = stu.getClass();
 }
}

Output results:
This is a static code block.
This is a code block.

2. If you don't have an I & tance object, only classes can be obtained directly by class literal constant class:

publicclass ReflectTest {
 @Test
 publicvoidreflect_class(){
 Class<?> clz = Student.class;
 }
}

Output results: no output!

In particular, we don't know the specific class or I & tance at coding time, so we can pass the static method of class:

publicclass ReflectTest {
 @Test
 publicvoidreflect_forName(){
 try {
 Class<?> clz = Class.forName("com.sky.javase.Student");
 } catch (ClassNotFoundException e) {
 e.printStackTrace();
 }
 }
}

Output results:
This is a static code block.

Getting class objects of a class can have several ways. But is difficult for, regardless of which one of the three ways is required, either an instance of a class or an instance object of the class, or a fully qualified name ( class name + class name, class loader, to class path ).




Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs