Java class members and instance members

Source: Internet
Author: User

Java class members (attributes and methods) can be divided into two types: instance members and class members.

1. What are instance members and class members?

Instance members belong to objects, that is, they belong to the object level. They include instance Member attributes (also known as instance member variables) and instance Member methods, only after an object is created can you access the instance Member attributes and instance Member methods.

Class Members belong to Classes. class members include class member attributes (also known as class member variables) and class member methods. You can use class names to directly invoke class member variables and call class member methods. Class Members do not need to be accompanied by objects, that is, they can reference class members even if they are not created. Class Members can also be referenced through objects. Class Members must be identified with the keyword static, also known as static members.

Ii. Differences between class variables and instance variables:
1. Declaration difference: If the static declaration is not used, the instance Member attribute is declared. If the static declaration is used, the class member attribute is declared.
For example:
Private Static X; // Class member variables

Private int y;// Instance member variable

2. Differences in Storage Structure: for instance variables, each instance member variable of each object is assigned a storage unit so that the instance member variables of different objects have different values; only one storage unit is allocated for class member variables, so that all objects use one class member variable together.

3. Differences in Reference Methods: instance member variables access objects through objects. class member variables belong to Classes and can be accessed either through objects or through classes.

Iii. Usage considerations:
Class method, you cannot access or use instance members.
For example:
Private int count; // Instance member variable
Private Static setcount (){
Count ++;
} // Class member Method
This is an incorrect usage. The correct usage is:
Private Static int count; // Class member variables
Private Static setcount (){
Count ++;

} // Class member Method

Iv. Examples
Create an animal, set the legs and kind attributes, and add the Count class member to record the number of animals. Create two sub-classes lion and fish that inherit the animal class, and set the class member count to record the number of such animals. DetailsCodeAs follows:

Animal. Java

Public class animal {private int legs; // instance member variable private string kind; // instance member variable Private Static count; // class member variable // non-parameter constructor public animal () {setlegs (4); count ++;} // public animal (int l) with parameter Constructor) {setlegs (l); count ++;} // sets the number of legs of an instance object. The instance Member method is public void setlegs (int l) {If (L! = 0 & L! = 2 & L! = 4) {system. Out. println ("Wrong Number of legs! "); Return;} Legs = L;} // gets the number of legs of an instance object. The instance Member method is public int getlegs () {return legs ;} // set the instance Member method public void setkind (string K) {kind = K;} // obtain the instance Member method Public String getkind () of the Instance Object Type () {return kind;} // obtain the number of object class member Methods public static int getcount () {return count ;}}

Lion. Java

Public class lion extends animal {Private Static int count; // set the number of Lion instances in the class member variable record public lion () {setlegs (4); setkind ("lion "); count ++;} // obtain the number of Lion class member Methods static public int getcount () {return count ;}}

Fish. Java

Public class fish extends animal {Private Static int count; // you can specify the number of fish instances in the class member variables. Public fish () {setlegs (0); setkind ("fish "); count ++;} // obtain the number of fish class member Methods static public int getcount () {return count ;}}

Main class, zoo. Java

Public class zoo {public static void main (string Arg []) {animal annimal = new animal (); fish fish1 = new fish (); fish fish2 = new fish (); fish fish3 = new fish (); fish fish4 = new fish (); lion lion1 = new Lion (); lion lion2 = new Lion (); system. out. println ("Total Animal:" + animal. getcount (); system. out. println ("total fish:" + fish. getcount (); system. out. println ("Total LION:" + lion. getcount ());}}

Running result:

Total Animal: 7

Total fish: 4

Total LION: 2

V. Summary

Instance members exist with objects, while class members exist with classes. You can directly use Class Members without an object, because the class is always there, but the instance members won't. If an instance Member is used in the class method. Therefore, an error is reported during Java compilation. The instance object can be a member of the category class, but note that when the data of the class member is changed, all objects that use this class member will be affected. Taking the previous example, if you go to the zoo's management room to change the number of animals, the people behind you will see this change. On the contrary, for instance members, each object has its own. For example, if you modify the legs attribute value of an object, the legs attribute value of other objects is not affected. Just like an animal in a zoo has a broken foot, other animals will not be affected.

Refer to Java se6 comprehensive learning

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: 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.