Class method,

Source: Internet
Author: User

Class method,

The class method belongs to all object instances in the following format:

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

Non-static variables cannot be accessed in class methods. Use class name. Class method name or object name. Class Method Name

Define a student class and count how much money students have paid?

Public class StuDemo {public static void main (String [] args) {Student stu1 = new Student (29, "aa", 365); Student stu2 = new Student (29, "bb", 360); System. out. println (stu2.getTotalFee () ;}// Student class Student {int age; String name; int comment; static int totalFee; public Student (int age, String name, int comment) {this. age = age; this. name = name; totalFee + = fee;} // returns the total tuition fee [This is a static method] // class variables are used in principle to access and operate public static int getTotalFee (){
Return totalworkflow ;}}

Class methods are class-related and public methods.

The instance method belongs to each object's individual method.

Class methods can be directly accessed by class name. Class Method Name

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