Classes, instances, and class objects in Java

Source: Internet
Author: User

Class

A class is an important concept in an object-oriented programming language, an abstract generalization of a thing that can contain some of the property definitions of the thing, as well as methods for manipulating properties. In object-oriented programming, we are all coded in class.

Instance

Simple understanding, is new, is the instantiation of the class, the creation of the class corresponding to the actual object, the class is just a description of things, and the instantiation is equivalent to this description of the new open up a piece of memory, you can change the area of the various attributes (member variables), of course, can also instantiate multiple areas, just different objects.

float x; (instance variable)

static int y; (class variable)

Class

Note that here C is capitalized, separate from the class concept, in Java, class is a real class, under Package Java.lang, There is such a Class.java file, which, like our own class, is a real class, and the class object is an instance of this class. In Java, the root of all classes is the object class, and class is no exception, it is a special class inherited from object, it can record the members of the class, interface and other information, that is, in Java, class is a class used to represent classes. (O (∩_∩) o a bit around, catch the key, class is a real class, you can create an instance of it, that is, the class object mentioned later in this article, also known as the class instance).

Java provides the following methods for getting class objects to classes:

1) Use the object instance to invoke the GetClass () method to get the class instance of the object;
2) using the class class static method Forname ("package name + Class name"), use the name of the classes to get a class instance

3) Use class name. class to get the class instance;

We know that the Java world is running on top of the JVM, and we write class code that, after the compiler compiles, generates a corresponding. class file for each class, which is the byte code that the JVM can load to execute. During runtime, when we need to instantiate any class, the JVM will first try to see if there is a class in memory, and if so, the class instance is created directly, and if not, the class is loaded according to the class name, when loading a class, or when the loader (class loader) DefineClass () is called by the JVM, it produces a class object (an instance of a class class) for this class, which is used to express the class, and all instances of the class share the class object, and are unique.

Summarize

In Java, a class is just a description of what the internal properties and interfaces are, and you can understand that a set of rules is defined, and that the class object is used in Java as an instance of the class's case, that is, the actual characterization of the class, which can be understood as a graphical representation of the rules, In this way, the JVM can be intuitively understood as a template, and the instantiation of a class object is the actual use of a piece of memory created by a template.

Example:

We use an example to understand the class instance, and for the sake of convenience, we create a new category with a deep point in the package name.

New Name.java (of course, the file is placed in the Com\dxjia\sample directory)

1 package com.dxjia.sample;  2  3 public class name {4     static int count = 0; 5     static {6         count++; 7         System.out.println ("name class loaded! Count = ["+ Count +"] "); 8     } 9 public     Name () {         System.out.println ("name Constructor called!");     }13 14}

Then create a new test main class in the root directory

 1 Import com.dxjia.sample.Name;       2 3 public class Test {4 static {5 Name mname; 6 System.out.println ("Test class Loaded"); 7} 8         9 public static void Main (string[] args) {System.out.println ("Entern Test Main ()"); 11 12 Name.class13 class mclasspointclass;14//Class.forName ("Full package name + Class name") class Mclassforname;1         6//New object after object. GetClass () mclassobjectpointclass1;18 class mclassobjectpointclass2;19 20 try {21//test class name. class22 Mclasspointclass = name.class;23 System.out.println ("M Classpointclass = "+ Mclasspointclass); 24 25//Test class.forname () Mclassforname = Cl             Ass.forname ("Com.dxjia.sample.Name"), System.out.println ("mclassforname =" + Mclassforname); 28 29 Test Object.getclass () name name1 = new name (); mClassObjectPointClass1 = Name1.getClass (); System.out.println ("MClassObjectPointClass1 =" + MClassObjectPointClass1); catch (Clas             Snotfoundexception e) {/TODO auto-generated catch block E.printstacktrace (); 36 RETURN;37} name2;40 System.out.println ("defined one name object"); New name (); System.out.println ("Name object instance done!"); MClassObjectPointClass2 = Name2.getclass (); if (Mclassforname = = Mclasspointcla SS47 && Mclasspointclass = = mClassObjectPointClass148 && MClassObjectPointClass1 = = MClassObjectPointClass2) {System.out.println ("All the Class object equal ..."); 50}51}52}

To compile them separately:

1 Javac com\dxjia\sample\name.java2 Javac Test.java

Perform:

1 Java Test

The code uses a static code block to experiment, a class to run, the JVM will do the following several things 1, class load 2, Link 3, initialize 4, instantiation, and the initialization phase is to initialize static variables and execute static methods, and always execute only once.

Output Result:

Test Class Loaded
Entern Test Main ()
Mclasspointclass = Class Com.dxjia.sample.Name
Name Class loaded! Count = [1]
Mclassforname = Class Com.dxjia.sample.Name
Name Constructor called!
MClassObjectPointClass1 = Class Com.dxjia.sample.Name
Defined one Name object
Name Constructor called!
Name Object Instance done!
All the Class object equal ...

The result shows that when you use the class name. class to get the class instance, it does not trigger the initialization of the classes, and The Class.forName method is triggered, of course, the instantiated object will certainly be triggered, but because the static code block is executed only once, so there is no more printing, the last printing, the class instance of the classes have only one.

Article Source: http://www.cnblogs.com/flyme/p/4571030.html

Classes, instances, and class objects in Java

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.