In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "how to understand classes, instances and Class objects in JAVA". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Class
Class is an important concept in object-oriented programming language. it is an abstract summary of a thing, which can contain some attribute definitions of the thing and the methods of manipulating the properties. In object-oriented programming, we all code with classes.
Example
Simply understand, new, is the instantiation of the class, create the actual object corresponding to this class, the class is just the description of things, and instantiation is equivalent to opening up a new piece of memory for this description, you can change various attributes (member variables) in this area, of course, you can instantiate multiple areas, just different objects.
Class
Note that C capitalizes here, distinguished from the class concept, in java, Class is a real class, under the package java.lang, there is such a Class.java file, it is a real class like our own defined class, and the Class object is an instance of this Class class.
In Java, the root of all classes is Object class, and Class is no exception, it is a special class inherited from Object, it can record class members, interfaces and other information, that is, in Java, Class is a class used to represent classes. (O (∩ _ ∩) o is a bit of a twist, to catch the point, Class is a real class for which you can create an instance, that is, the Class object mentioned later in this article, also known as the Class instance.
Java provides the following ways to get the Class object of a class:
1) use the object instance to call the getClass () method to get the Class instance of the object
2) use the static method forName of the Class class ("package name + class name") to get an instance of Class with the name of the class
3) use the class name .class to obtain Class instance
We know that the java world runs on JVM, and the class code we write, after being compiled by the compiler, generates a corresponding .class file for each class, which is the bytecode that JVM can load and execute. During runtime, when we need to instantiate any class, JVM first tries to see if the class is in memory, and if so, creates an instance of the class directly If not, the class is loaded according to the class name. When a class is loaded, or when the defineClass () of the loader (class loader) is called by JVM, a Class object (an instance of the Class class) is generated for the class to express the class. All instances of the class share the Class object and are unique.
Summary
In java, a class is just a description of information, indicating what internal attributes and interfaces there are, which you can understand as defining a set of rules; and the Class object is used in java to express an example of the situation of the class, that is, the actual representation of the class, which can be understood as a diagram of the rules, so that JVM can understand it intuitively and can be seen as a template. The instantiated object of the class is the actual use of a piece of memory opened up by the template.
Example:
We use an example to understand the Class instance. For convenience, we create a new class with a deeper package name.
Create a new Name.java (of course, the file will be placed in the com\ dxjia\ sample directory)
Package com.dxjia.sample;public class Name {static int count = 0; static {count++; System.out.println ("Name Class Loaded! count = [" + count+ "]");} public Name () {System.out.println ("Name Constructor called!");}}
Then create a new Test main class in the root directory
Import com.dxjia.sample.Name;public class Test {static {Name mName; System.out.println ("Test Class loaded");} public static void main (String [] args) {System.out.println ("entern Test main ()"); / / Name.class Class mClassPointClass; / / Class.forName ("full package name + class name") Class mClassForName; / / new object, object .getClass () Class mClassObjectPointClass1; Class mClassObjectPointClass2; try {/ / test class name .class class = Name.class System.out.println ("mClassPointClass =" + mClassPointClass); / / Test Class.forName () mClassForName = Class.forName ("com.dxjia.sample.Name"); System.out.println ("mClassForName =" + mClassForName); / / Test Object.getClass () Name name1 = new Name (); mClassObjectPointClass1 = name1.getClass (); System.out.println ("mClassObjectPointClass1 =" + mClassObjectPointClass1);} catch (ClassNotFoundException e) {/ / TODO Auto-generated catch block e.printStackTrace (); return;} Name name2 System.out.println ("defined one Name object"); name2 = new Name (); System.out.println ("Name object instance done!"); mClassObjectPointClass2 = name2.getClass (); if (mClassForName = = mClassPointClass & & mClassPointClass = = mClassObjectPointClass1 & & mClassObjectPointClass1 = = mClassObjectPointClass2) {System.out.println ("all the Class object equal...");}
Compile them separately:
Javac com\ dxjia\ sample\ Name.java javac Test.java
Execute:
Java Test
The static static code block is used in the code to experiment. When a class runs, JVM does the following things: 1, class loading 2, link 3, initialization 4, instantiation; and the initialization phase does the work of initializing static variables and executing static methods, which is always executed only once.
Output result:
Test Class loadedentern Test main () mClassPointClass = class com.dxjia.sample.NameName Class Loaded! Count = [1] mClassForName = class com.dxjia.sample.NameName Constructor calledclassmClassObjectPointClass1 = class com.dxjia.sample.Namedefined one Name objectName Constructor calledroomName object instance examples all the Class object equal...
From the results, we can see that when you get a Class instance using the class name .class, the initialization of the class will not be triggered, and the Class.forName method will trigger, of course, the instantiated object will certainly be triggered, but because the static code block will only be executed once, there will be no more printing, and the final print shows that there is only one Class instance of a class.
So much for the introduction of "how to understand classes, instances and Class objects in JAVA". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.