Java Learning Lesson nineth (two static keywords)

Source: Internet
Author: User
Tags ldap

keyword two, static


Characteristics:
Static used to decorate members (member functions and member functions)

The presence of static, which takes precedence over the existence of the object, loads as the class loads

Static modified member, shared by all objects

Static can be called directly by the class name, format: System.out.println (person.country);: class name. static member

Use details

Static methods can only access static members, which are members of static adornments, static int data;

A static method cannot write this, super:this represents the object that is currently calling the method.

Main function (main) is static: public static void Main (string[] args)


Import Java.util.scanner;import javax.naming.ldap.sortcontrol;class person{string name;//member variable/instance variable    static String country = "CN";//static variable/class variable public void Show () {System.out.print (country+ ":" +name);}} public class Main{public static void Main (string[] args) {person BLF = new person (); Scanner cin =  new Scanner (system.in);//blf.name = Cin.nextline (); Blf.name = "BLF2"; Blf.show (); System.out.println (person.country);//static sharing, can be directly with the class name output                                   //But not misuse, some data is unique, not shared        //such as Chinese, nationality is shared, But the name is unique Cin.close ();}}


The difference between a member variable and a static variable:


1. The life cycle of the two is different. A static life cycle long


member variables exist as objects are created and released as objects are released, that is, the existence of member variables depends on the existence of the object
static variables are loaded as the class is loaded, and disappear as the class disappears


2. Called differently
member variables can only be called by an object
static variables may be called by the object, can be called with the class name, so there are static decorated members, preferably called with the class name, clear, and do not create the object

3.
A member variable is called an instance variable
A static variable is called a class variable


4. They are stored in different locations.
member variables are stored in the object of the heap memory, so it is also called the object's unique data (the heap will not be able to put variables, only entities, entities can have variables)
Static variable data is stored in the static area of the method area (data area/shared area), so it is also called the shared data of the object


static in use:


1. Access has limitations: Static decorated methods can access only static decorated members


error code:
Import Java.util.scanner;import javax.naming.ldap.sortcontrol;class person{string name;    static String country = "CN";p ublic static void Show () {System.out.print (country+ ":" +name);//Because there are no static decorated members Name//sys Tem.out.print (country+ ");//So cannot have name}}public class main{static int data = 9;public static void Main (string[] args) {Perso n BLF = new Person (); Scanner cin =  new Scanner (system.in);//blf.name = Cin.nextline (); Blf.name = "Hello"; Blf.show (); Cin.close ();}}

Therefore, the static decorated members can be called directly with the class name without the object, because the object does not exist, so name does not exist.
(Non-static access to both static and non-static)



2. The main function is static.

public class main{static int data = 9;//static decorated member variable public static void Main (string[] args) {       show ();} public void Show ()//here if you want to call, you must use the static modification method, data is not static, called unique data        //public static void Show ()//This way can access the static decorated data{ SYSTEM.OUT.PRINTLN (data);}}

You want to access the unique data, so you want to call a show method that you can invoke with an object

public class Main{int data = 9;//static decorated member variable public static void Main (string[] args) {main BLF = new Main ();       Blf.show ();} public void Show () {System.out.println (data);//can also System.out.println (main.data);//main.  can be omitted}}

Therefore, the main function is used to direct other objects to work, to encapsulate the code in the function, and then encapsulate the function into each class, main only need to create the object, command so object work can


Not finished ...


Java Learning Lesson nineth (two static keywords)

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.