Java static modifier function

Source: Internet
Author: User


Package com.fish.object;/*static (static, modifier) static modifier member variable: When static modifies a member variable, the data for that member variable is a shared data. How static member variables are accessed: mode one:   Access by using objects.   Object. Property Name Method Two: You can access it by using the class name. The class name. Note:1.  non-static member variables cannot be accessed directly by the class name, only by using the object. 2.  do not use the static modifier to facilitate access to member variables, and you must use the static adornment only if the data is shared  . Static Modification method (statically member method): Access mode: Method One: You can use the object for access. object. Static function name (); mode two: You can access it by using the class name. Class name. static function name. The recommended use is to have the class name directly accessible to static members. The difference between a static member variable and a non-static member variable:1.  the difference in function:1.  the function of a static member variable to share a   data for all objects to use. The role of 2.  non-  static member variables is to describe the public properties of a class of things. The difference between the number of 2.  and the storage location:1.  static member variables are storage methods   area memory, and only one copy of the data exists. 2.  non-static member variables are stored in heap memory, and N objects have n copies of data.  3.  life cycle Differences:1.  static member variable data is present as the class is loaded and disappears as the class file disappears. 2. Non-static member data is present as the object is created and disappears as the   object is reclaimed by the garbage collector.   Static functions to note:1.  A static function can be called by invoking a class name or an object, and a non-static function can only be invoked using an object. 2.  static functions can directly access static members (static methods and static methods), but cannot directly access non-static members. Cause: A static function can be called directly using the class name, and there may not be an object at this time, and non-static   member data exists with the existence of object  . 3.  non-static functions are directly accessible to static and non-static members. Cause: A non-static function can only be called by an object, and when the object exists, the static data is already there, and the non-static data exists as the object is created. 4.  static function cannot appear this or Super keyword. Cause: Because a static function can be called using the class name, once called with the class nameThere is no object at this time, and the This keyword represents a function   caller object, which creates a conflict. The life cycle of static data: Static member variable data takes precedence over object existence. When does static modify a function? If a function does not have direct access to non-static members, then the static modifier can be used.   methods commonly used for tool types static functions cannot access non-static members?   Static functions can also access non-  static data as long as there are objects in it. Just can't access it directly. */class student{    string name; //name     staticstring  country =  "China";  //nationality        //static code block   : Static code blocks are executed as soon as the Student.class file is loaded into memory.     static{        system.out.println ("Static code block executed ... ");     }    //constructor     public student (String  name) {        this.name = name;     }    //non-static member function     public  void study () {         SYSTEM.OUT.PRINTLN ("Study hard" +this);    }     //static function     public static void sleep () {  // The static method and the non-static method of the bytecode file are both in-memory  .   Just static member variable data is preferable to object existence only.         student s= new student ("Iron egg");         system.out.println (s.name+ "sleep ...");     }}class demo2{     public static void main (String[] args)       {        student.sleep ();         //student s = new student ("Dog Doll");     }}


package com.fish.object;/* Requirements: A tool class that writes an array. Arrays.tostring () [1,2,3,4];sort () *///Array Tool class Class arraytool{    public static  string tostring (Int[] arr) {        string result   =  "";         for (int i = 0;   i < arr.length ; i++) {             if  (i==0) {                 result+= "[" +arr[i]+ ",";             }else if (i== (arr.length-1)) {                 result+= arr[i]+ "]";             }else{            &nbSp;   result+=arr[i]+ ",";             }        }        return  result;    }    public static void sort (int[]  arr) {        for (int i = 0; i <  arr.length-1 ; i++) {             for (int j = i+1 ; j<arr.length ; j++) {                 if (Arr[i]>arr[j]) {                     int  temp = arr[i];                     arr[i] = arr[j];                     arr[j] = temp;                 }             }         }    }}class  demo3{    public static void main (String[] args) {         int[] arr = {12,1,456,165};         //arraytool tool = new arraytool ();         arraytool.sort (arr);        string info =  Arraytool.tostring (arr);         system.out.println ("Elements of the array:" + info );     }}


The Package com.fish.object;import java.util.scanner;/*main function is detailed:public :  public.   permissions are the largest and can be accessed under any circumstances.  private   reason:  to ensure that the JVM can access the main method under any circumstances. static:   Static. Static allows the JVM to invoke the main function more conveniently. You do not need to call through an object.  1, need to create an object call 2, the JVM does not know how to create an object, because there are some parameters needed to create the object, parameters passed what is?void:   there is no return value.   Since the data returned is   to &NBSP;JVM, it is meaningless for the JVM to use this data. So you don't have to. The name of the main:  function.     Note:  main is not a keyword, just a special function name that the JVM can recognize. &NBSP;ARGUMENTS&NBSP: Worry about some programs that require parameters at startup. */class demo4 {    public static  void main (String[]  args)      {        system.out.println ("The length of the array: (+ args.length);         for (int i = 0 ;  i <args.length ; i++) {             system.out.print (args[i]+ ","); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;}&NBsp;       scanner scanner = new scanner (System.in);     }}


This article from "Small Fish Blog" blog, declined reprint!

Java static modifier function

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.