Class variables, class methods, instance variables, and instance methods in java

Source: Internet
Author: User
Tags instance method modifier

Class variables, class methods, instance variables, and instance methods in java
Before understanding the class variables and class methods, read the following code:


Class person {int age; string name; static int totalfee; public void showname () {system. out. print (this. name);} public static void showtotalloud () {system. out. print (total.pdf );}}
Three variables are defined in the person class. Two variables are the direct type + variable name, and the keyword static is added before the 3rd variables.

A class variable is also called a static variable, that is, a static variable is added before the variable;
Definition of class variables: access modifier static data type variable name
Instance variables are also called object variables, that is, variables without static;
So what are their differences?
Class variables are shared by all objects. One of the objects changes its value. Other objects get the changed result, while the instance variables belong
The object is private. An object changes its value without affecting other objects;
Also, both class variables and instance variables are global variables.

Two methods are also defined in the person class. The difference is that 2nd methods also have multiple static keywords.
This method is a class method. The class method is a class-related and public method.
The instance method belongs to each object.
Class method definition: access modifier static return data type 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.