Home > Java > Javagetting Started > body text

What is the difference between class and Class in Java?

王林
Release: 2020-07-17 16:26:01
forward
4113 people have browsed it

What is the difference between class and Class in Java?

Difference:

(Recommended tutorial: java introductory tutorial)

class is a keyword in Java, such as public class Xxx or class Xxx, used when declaring Java classes.

Class is a class, which is equivalent to an abstraction and collection of classes.

Class introduction:

Class is a class, which is in the java.lang package.

What is the difference between class and Class in Java?

Its constructor is a private attribute, so we cannot directly create a new Class object. "Private constructor. Only the Java virtual machine creates class objects. This constructor is not used and prevents the generation of the default constructor."

What is the difference between class and Class in Java?

How to get the Class object?

1. Obtain the Class object through the getClass() method

The getClass() method is part of the Object class. If we have created an object of a certain type, we can obtain the Class object of that type through the getClass() method.

package Task;
import org.junit.Test;

public class Try0 {
    @Test
    public void toTry() throws ClassNotFoundException {
//        //forName方法:参数为其类的路径
//        Class a = Class.forName("Task.Try1");
//        System.out.println(a);

        //通过对象得到类
        Try1 try1 = new Try1();
        Class b = try1.getClass();
        System.out.println(b);
    }
}

class Try1{

}
Copy after login

Running results:

What is the difference between class and Class in Java?

(Video tutorial recommendation: java video tutorial)

2. Through forName () method to obtain the Class object

Class.forName method is a static method of the Class class. So you can get the Class object directly through Class.forName ("path to the class").

package Task;
import org.junit.Test;

public class Try0 {
    @Test
    public void toTry() throws ClassNotFoundException {
        //forName方法:参数为其类的路径
        Class a = Class.forName("Task.Try1");
        System.out.println(a);
    }
}

class Try1{

}
Copy after login

Run result:

What is the difference between class and Class in Java?

3. Class.class obtains the Class object (class literal constant)

package Task;
import org.junit.Test;

public class Try0 {
    @Test
    public void toTry() throws ClassNotFoundException {
//        //forName方法:参数为其类的路径
//        Class a = Class.forName("Task.Try1");
//        System.out.println(a);

//        //通过对象得到类
//        Try1 try1 = new Try1();
//        Class b = try1.getClass();
//        System.out.println(b);

        //类字面常量
        Class c = Try1.class;
        System.out.println(c);
    }
}

class Try1{

}
Copy after login

Run result :

What is the difference between class and Class in Java?##

The above is the detailed content of What is the difference between class and Class in Java?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:csdn.net
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template