Java Object-oriented programming (iii)-class variables, class methods

Source: Internet
Author: User

1. What is a class variable?

A class variable is a variable that is shared by all objects of that class, and when any object of that class accesses it, the same value is taken, and the same variable is modified when any object of that class modifies it.

How do I define a class variable?

Definition syntax:

access modifier static data type variable name;

How do I access class variables?

class name. Class variable name or object name. Class variable name

Case: There are a group of children to play snowman, sometimes new children to join, how to know how many people are playing? Use object-oriented thinking to write programs to solve.

 Public classdemo113{ Public Static voidMain (String []args) {/*non-object-oriented method int total=0;        Child Ch1=new Child (3, "Niuniu");        Ch1.joingame ();        total++;        Child Ch2=new Child (4, "small");        Ch2.joingame ();     total++; */       //Object-oriented approachChild ch1=NewChild (3, "Dragons");        Ch1.joingame (); Child CH2=NewChild (4, "just son");        Ch2.joingame (); Child CH3=NewChild (5, "Kankan");        Ch3.joingame (); System.out.println ("Total =" +child.total); }}//Define Child Classesclasschild{intAge ;    String name; //Total is a static variable, so it can be accessed by any object    Static intTotal=0;  PublicChild (intage,string name) {         This. age=Age ;  This. name=name; }     Public voidJoingame () { total++; System.out.println ("One of the children has joined."); }}

2. What is a class method and why is there a class method?

A class method belongs to all object instances in the following form:

access modifier static data return type method name () {}

Note: non-static variables (class variables) cannot be accessed in class methods.

Use:

Class Name. class method name or object name. class Method Name

Statically static methods can access static variables, cannot access non-static variables (class variables), non-static methods can access nonstatic variables (class variables), and can also access static static variables.

Case: Statistical total tuition Code, deepening static static variable access by static method [Demo115.java]

 Public classdemo115{ Public Static voidMain (String []args) {//Create a studentStu stu1=NewStu ("AA", 340); Stu STU2=NewStu ("AA", 240);    System.out.println (Stu.gettotalfee ()); }}//Student Classclassstu{intAge ;    String name; intfee; Static intTotalfee;  PublicStu (intAge,string name,intfee) {         This. age=Age ;  This. name=name; Totalfee+=fee; }    //return total tuition fee [This is a class method (static method)]//rules in Java: Class variables in principle to access or manipulate with class methods     Public Static intGettotalfee () {returnTotalfee; }}

3. Other issues

1. When to use class variables

Example [Demo115.java]: Define student class, how much does the student pay? Use a class variable to represent a common property

2. Class variables differ from member variables:

A. Adding static is called a class variable or a static variable, otherwise called a member variable

B. Class variables are class-dependent, public-owned properties

C. member variables belong to each individual object property

D. class variables can be accessed directly by [class name. class variable name]

3. When to use the class method

Example [Demo115.java]: Define student class, how much does the student pay?

4. The class method differs from the class variable:

A. Class methods belong to class-related, public methods

B. Methods of member methods that belong to each individual object

C. class methods can be accessed directly through the [class name. class method Name]

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.