search
HomeJavaJavagetting StartedWhat is the difference between class and Class in Java?

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{

}

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{

}

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{

}

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!

Statement
This article is reproduced at:csdn. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.