Java Basic Class 09 data and Class methods

Source: Internet
Author: User
Tags class definition

Vamei Source: Http://www.cnblogs.com/vamei Welcome reprint, Please also keep this statement. Thank you!

We have always defined classes (class) for the purpose of producing objects. An object is a functional entity, and a class is a type classification of an object. This is a basic concept of object-oriented.

In Inheritance (inheritance), we regard classes as a subject that can be expanded, which improves our understanding of "class".

The class itself has many places to discuss. We will continue in depth.

static data member

There is some data to describe the state of the class. For example, the human class, we can use "population" to represent the total number of objects of the human class. "Population" directly describes the state of a class, not an object.

The population of the human category is 8

All objects of the class share the population data. Such data is referred to as class data members (classes field).

In the class definition, we use the static keyword to declare class data members, such as:

Classhuman{
/**
* Constructor
*/
Public Human (int h)
{
This.height = h;
}
/*** Accessor*/PublicIntGetHeight () {ReturnThis. Height; }/*** Mutator*/Publicvoid Growheight (Inth) {This.height =this.height + H;} /** breath */public void breath () {System.out.println ("hu...hu ... ");} private int height;
private static< Span style= "color: #0000ff;" > int population; public static Boolean Is_mammal = true

We have defined two classes of data members: Population and is_mammal. All human objects share one population data, and the is_mammal (which are mammals) of any human object are true.

Class data members should also set access permissions. For class data members that are declared public, you can use Class.field or Object.field (if there is an object of that class) to access directly from outside the way. Both of these access methods are reasonable, because a class data member can be considered a property of a class and can be considered a property shared by all members. If a class data member is defined as private, the class data member can only be accessed from within the class.

(The Is_mammal is set to public, just for demonstration purposes.) It is dangerous to do so, in case someone uses human.is_mammal=false, all humans suffer. Or that basic principle, try to set the data to private. )

static Method

We can also have a class method, that is, a method declared as static. A class method represents an action that a class can implement, where the operation does not involve a specific object. If a method is declared static, it can only invoke static data and methods, not non-static data and methods.

In fact, in the static method, there will be no implicit pass of this and super parameters. We have no way of referencing the data and methods that belong to the object (which is exactly what we want).

In the aggregate, we have the following relationship:

A dashed red line indicates that it cannot be accessed. In other words, the object's data cannot be accessed in a class method.

Here we add a static method getpopulation (), which returns the static data population:

Classhuman{
/**
* Constructor
*/
Public Human (int h)
{
This.height = h;
}
/*** Accessor*/PublicIntGetHeight () {ReturnThis. Height; }/*** Mutator*/Publicvoid Growheight (Inth) {This.height =This.height +H }/*** Breath*/PublicvoidBreath () {System.out.println ("hu...hu ...")); }private int height; Span style= "color: #008000;" >/* * static method, Access population */public static intreturn human.population;} private static intprivate static boolean Is_mammal = true

When invoking a class method, we can call it either by Class.method () or by Object.Method (). For example, use the following test class tests:

Class test{    voidnew Human (160); System.out.println (Aperson.getpopulation ()); }}

We call the class method Getpopulation () outside of the class definition in two ways .

Object methods Modify class data

We see that the object method can access the class data. This is a very useful concept. The state of a class can vary with the object. For example, "population", which should increase by 1 as a result of an object. We can modify the "population" data of the class in the method of the object. We access the class data members below in the constructor method. The construction method here is a non-static method, that is, the object's method:

Classhuman{/*** Constructor*/Public Human (Inth) {This.height =H Human.populatin = human.population + 1; }/*** Accessor*/PublicIntGetHeight () {ReturnThis. Height; }/*** Mutator*/Publicvoid Growheight (Inth) {This.height =This.height +H }/*** Breath*/PublicvoidBreath () {System.out.println ("hu...hu ...")); }private int height; Span style= "color: #008000;" >/* * static method, Access population */public static intreturn human.population;} private static intprivate static boolean Is_mammal = true      

Each time we create an object, the class data is modified by the way the object is constructed, adding 1 to the population class data. In this way, population can reflect the total number of objects that belong to the class in real time (you can create multiple objects in test and then print human.population).

In addition to the above example of the construction method, we can also access the class data in the normal object method.

Final

The basic meaning of the final keyword is that the data/method/class cannot be changed.

    • Final basic type of data: fixed value (constant value), can only be assigned once and can no longer be modified.
    • Final method: The method cannot be overwritten. The private method defaults to the final method.
    • Final class: The class cannot be inherited.

An object of a normal type can also have the final keyword, which means that the object reference (reference) cannot be modified. That is, the reference can only point to an object. However, the contents of the object can be changed (similar to the static pointer in C). We'll cover the object references later.

If a basic type of data is both final and static, it is a fixed value that stores only one copy. This is ideal for storing some constants, such as Pi.

Summary

static field, static method

Class.static_method ()

Final

Java Basic Class 09 data and Class methods

Related Article

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.