JAVA class and object (6) ------ differences between instance variables and class variables, differences between instance methods and class methods, and java ------

Source: Internet
Author: User

JAVA class and object (6) ------ differences between instance variables and class variables, differences between instance methods and class methods, and java ------
Instance variable class variable (static variable)

class TiXing{    float up,height;    static float down;        TiXing(float x,float y,float z){        up=x;        height=y;        down=z;    }}public class ep3_9{    public static void main(String args[]){        TiXing one=new TiXing(1,2,3);        System.out.println("one's down is:"+one.down);        TiXing two=new TiXing(4,5,6);        System.out.println("one's down is:"+one.down);        System.out.println("two's down is:"+two.down);            System.out.println("TiXing's down is:"+TiXing.down);    }}

 

 

 

Instance method and class method access to instance variables and class variables

The instance method can operate the instance variables of the current object, or the class variables. The instance method is called by the instance object.

Class methods cannot access instance variables. They can only be class variables. The class method is called by the class name or instance object. This or super keyword cannot appear in class methods.

Class TiXing {private float up, height; private static float down; TiXing (float x, float y, float z) {up = x; height = y; down = z ;} public void display () {System. out. println ("up is:" + up + "height is:" + height + "down is:" + down);} public static void change (float number) {down = number; // System. out. println ("height:" + height); // error} public class ep3_9 {public static void main (String args []) {TiXing one = new TiXing (1, 2, 3); one. display (); TiXing two = new TiXing (4,5, 6); one. display (); two. display (); // TiXing. display (); // error one. change (101); one. display (); two. change (102); two. display ();}}

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.