The difference between a Java class and an object---an instance variable and a class variable, the difference between an instance method and a class method

Source: Internet
Author: User
Tags float number instance method


Instance variable
    • Instance variables are declared in a class, but outside of methods, construction methods, and statement blocks;
    • When an object is instantiated, the value of each instance variable is then determined;
    • Instance variables are created when the object is created and destroyed when the object is destroyed;
    • The value of the instance variable should be referenced by at least one method, construct method, or statement block, so that the external can obtain instance variable information through these methods;
    • Instance variables are visible to methods, construction methods, or block statements in a class. In general, you should set the instance variable to private. You can make instance variables child classes visible by using the access modifier
    • The instance variable has a default value. The default value for a numeric variable is 0, the default value for a Boolean variable is false, and the default value for the reference type variable is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method;
    • Instance variables can be accessed directly from the variable name. However, in static methods and other classes, you should use the fully qualified name: Obejectreference.variablename.

class variables (static variables)

Class variables, also known as static variables, are declared in the class with the static keyword, but must be outside the method construction method and statement block.

No matter how many objects a class creates, the class has only one copy of the class variable.

Static variables are seldom used except when declared as constants. Constants are variables declared as public/private,final and static types. Constants cannot be changed after initialization.

Static variables are stored in a static storage area. are often declared as constants, and static declaration variables are seldom used alone.

Static variables are created at the beginning of the program and are destroyed at the end of the program.

has similar visibility to instance variables. However, in order to be visible to the consumer of a class, most static variables are declared as public types.

The default value is similar to the instance variable. The default value for numeric variables is 0, the Boolean default is False, and the reference type default value is null. The value of a variable can be specified at the time of declaration, or it can be specified in a constructor method. In addition, static variables can be initialized in static statement blocks.

Static variables can be accessed by:classname.variablename .

When a class variable is declared as public static final type, the class variable name must use uppercase letters. If the static variable is not public and final, it is named in the same way as the instance variable and the local variable.



1 class TiXing {

 2 float up, height;
 3 static float down;
 4
 5 TiXing (float x, float y, float z) {
 6 up = x;
 7 height = y;
 8 down = z;
 9     }
10}
11
12 public class ep3_9 {
13 public static void main (String args []) {
14 TiXing one = new TiXing (1,2,3);
15 System.out.println ("one ‘s down is:" + one.down);
16 TiXing two = new TiXing (4,5,6);
17 System.out.println ("one ‘s down is:" + one.down);
18 System.out.println ("two ‘s down is:" + two.down);
19
20 System.out.println ("TiXing ‘s down is:" + TiXing.down);
twenty one     }
twenty two }
View Code
Instance method and class method access to instance variables and class variables

Instance methods can operate on instance variables of the current object, as well as class variables. Instance methods are called by instance objects.

Class methods cannot access instance variables, only class variables. Class methods are called by class names or instance objects. The this or super keywords cannot appear in class methods
 1 class TiXing {
 2 private float up, height;
 3 private static float down;
 4
 5 TiXing (float x, float y, float z) {
 6 up = x;
 7 height = y;
 8 down = z;
 9     }
10 public void display () {
11 System.out.println ("up is:" + up + "height is:" + height + "down is:" + down);
12}
13 public static void change (float number) {
14 down = number;
15 //System.out.println("height:"+height);//Error
16}
17}
18
19 public class ep3_9 {
20 public static void main (String args []) {
21 TiXing one = new TiXing (1,2,3);
22 one.display ();
23 TiXing two = new TiXing (4,5,6);
24 one.display ();
25 two.display ();
26
27 //TiXing.display();//Error
28 one.change (101);
29 one.display ();
30 two.change (102);
31 two.display ();
32}
33}
View Code
Note: This article is transferred from http://www.cnblogs.com/scf141592/p/5726347.html, please indicate the source of the author when reprinting.

JAVA classes and objects-the difference between instance variables and class variables, the difference between instance methods and class methods

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