JAVA basics/26th: class and object/JAVA class attributes, static attributes, java static

Source: Internet
Author: User

JAVA basics/26th: class and object/JAVA class attributes, static attributes, java static

When an attribute is modified statically, it is called a class attribute or a static attribute.
When an attribute is declared as a class attribute, all objects share a value.
Comparison with object attributes:
The object attribute values of different objects may be different.
For example, galun's hp and Timo's hp are different.
However, the class attribute values of all objects are the same.

1. class attributes:

Class property: Also called static property
Object Property: Also called instance property, non-static property
If an attribute is declared as a class property, all objects share this value.
Set a category attribute for a hero called "copyright". No matter how many specific heroes there are, the copyrights of all the heroes belong to Riot Games.

Package charactor; public class Hero {public String name; // instance attribute, object attribute, non-static attribute protected float hp; static String copyright; // class attribute, static attribute public static void main (String [] args) {Hero garen = new Hero (); garen. name = "Galen"; Hero. copyright = "copyright owned by Riot Games"; System. out. println (garen. name); System. out. println (garen. copyright); Hero teemo = new Hero (); teemo. name = "Timo"; System. out. println (teemo. name); System. out. println (teemo. copyright );}}

2. Category attributes:

Two methods are available for category attributes.
1. Object. class attributes

teemo.copyright

2. Class. class attributes

Hero.copyright

Both methods can be used to modify and obtain the class attributes. However, we recommend that you use the second class. Class Attribute method to better understand the semantics.

When to use object attributes and when to use class attributes:

1. If an attribute is different for every hero, such as name, this attribute should be designed as an object attribute because it follows the object and the name of each object is different.
2. If a property is shared by all heroes, it should be designed as a class property. For example, the maximum blood volume of all heroes is 9999,

It does not take different values for different heroes. Such attributes are suitable for class attributes.

3. Class methods:

Class Method: Also called static method
Object method: also called the instance method, non-static method
To access an object method, you must create an object on the premise that
Objects method. You can directly access

Package charactor; public class Hero {public String name; protected float hp; // instance method, object method, non-static method // only an object is required to call public void die () {hp = 0;} // class method, static method // you can directly call public static void battleWin () {System through the class. out. println ("battle win");} public static void main (String [] args) {Hero garen = new Hero (); garen. name = "Galen"; // you must have an object to call garen. die (); Hero teemo = new Hero (); teemo. name = "Timo"; // no object is required, and Hero is called directly through the class. battleWin ();}}

4. Call methods:

Like the callback class property, there are two ways to call the class method.
1. Object. Class Method

garen.battleWin();

2. Class. Class Method

Hero.battleWin();

Both methods can call the class method, but we recommend that you use the second class. Class method, which is more in line with the semantic understanding.
In many cases, there are no examples, such as the method for obtaining random numbers used in the previous exercise.

Math.random()

Random () is a class method that is called directly through the Math class. No Math instance exists.

When to design object methods and when to design class methods:

If an object property is called in a method, for example:

public String getName(){    return name;}

The name attribute is an object property. It makes sense only when a specific object exists. If the object attribute is accessed in the method, this method must be designed as an object method.
If a method does not call any object property, you can consider designing it as a class method, for example:

Public static void printGameDuration () {System. out. println ("10 minutes and 50 seconds have been played ");}

PrintGameDuration: How long has it been playing? It is not associated with a specific hero. All heroes are the same. This method is more functional
Just like random numbers, random () is a functional method.

5. Object attribute initialization:

Three types of Object Property initialization are available:
1. initialization when declaring this attribute
2. Initialization in Constructor
3. initialize the block

Package charactor; public class Hero {public String name = "some hero"; // initialize protected float hp; float maxHP; {maxHP = 200; // initialization block} public Hero () {hp = 100; // initialization in constructor}}

6. Class Attribute initialization:

Class Attribute initialization has two types
1. initialization when declaring this attribute
2. Static initialization Block

Package charactor; public class Hero {public String name; protected float hp; float maxHP; // public static int itemCapacity = 8; // initialize static {itemCapacity = 6 during Declaration; // initialize the static initialization block} public Hero () {} public static void main (String [] args) {System. out. println (Hero. itemCapacity );}}

 

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.