Java Note 4__static keyword/object array variable parameters for/foreach/method

Source: Internet
Author: User

/*** Static keyword: a modifier attribute (which is essentially a global variable), a method (calling this method without an object of this class), or a class. * 1.static method can only call static method * 2.static method can only access static data * 3.static method cannot refer to this or super in any way*/ Public classMain { Public Static voidMain (string[] args) {A a=NewA (); A.val = 2; A A1=NewA (); A1.val = 3; System.out.println (A.val+" "+a1.val); b b=NewB (); B.val = 2; B B1=NewB (); B1.val = 3; System.out.println (B.val+ "" +b1.val);//B and B1 have the same share ValA.say (); }}classa{intval = 1;  Public Static voidsay () {System.out.println ("Hello~~"); }}classb{Static intval = 1;//static data does not belong to the object, belonging to the class}

Importjava.lang.String;Importjava.util.Arrays;ImportJava.util.Scanner;/*** Object Array*/ Public classMain { Public Static voidMain (string[] args) {Monkeymanager mm=NewMonkeymanager (); Mm.add (NewMonkey ("1")); Mm.add (NewMonkey ("2")); Mm.add (NewMonkey ("2")); Mm.add (NewMonkey ("4")); Mm.delete ("2");                Mm.list (); Monkey Temp1=NewMonkey ("1");        Mm.update (TEMP1);    Mm.list (); }}classmonkeymanager{PrivateMonkey[] ms =NewMonkey[3]; Private intCount = 0;  Public voidAdd (Monkey m) {if(count<ms.length) {Ms[count++] =m; System.out.println ("Successful Add Monkey:" +m.name); }        Else{System.out.print ("The space is full!" To expand .... "); intNewlen = (ms.length*3)/2+1; Ms=arrays.copyof (MS, Newlen); System.out.println ("The extended length is:" +Newlen); Ms[count++] =m; System.out.println ("Successful Add Monkey:" +m.name); }    }     Public voidlist () { for(inti=0;i<count;++i) Ms[i].say (); }     Public voidDelete (String name) {BooleanFlag =true;  while(flag) {flag=false;  for(intI=0;i<count;++i)if(ms[i].name.equals (name)) { for(intJ=I;J&LT;COUNT-1;++J) ms[j]=ms[j+1]; Ms[count-1] =NULL; --count; System.out.println ("Successful Delete monkey:" +name); Flag=true;  Break; }        }    }     PublicMonkey Find (String name) { for(intI=0;i<count;++i)if(ms[i].name.equals (name))returnMs[i]; return NULL; }     Public voidUpdate (Monkey m) {Monkey s=find (M.name); if(s!=NULL) {S.name= "HelloWorld"; }    }}classmonkey{String name;  PublicMonkey (String name) { This. Name =name; }     Public voidsay () {System.out.println ("I ' m" +name); }}

 Public classMain { Public Static voidMain (string[] args) {/*** foreach Syntax * for (type variable name: Array or collection) {//output operation}*/string[] Names= {"1", "2", "3", "4"};  for(String i:names) System.out.println (i); /*** Variable parameter of method * 1. Only one * 2. When there are multiple parameters, the variable parameter is placed in the last*/method1 (names); METHOD2 (123, "A1", "A2", "A3"); METHOD2 (123, names); }     Public Static voidmethod1 (string[] na) { for(String i:na) System.out.println (i); }     Public Static voidMETHOD2 (intX,string. Na) {//parameter is a variable parameter (only one)System.out.println ("x=" +x);  for(String i:na) System.out.println (i); }}

Java Note 4__static keyword/object array variable parameters for/foreach/method

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.