Java Class Basics

Source: Internet
Author: User

classPerson {PrivateString name; Private intAge ; {        //constructs a code block: You can initialize the "all" object//generic initialization code in a constructor function//execution Order: 1. Static code block, 2. Construct code block, 3. ConstructorSYSTEM.OUT.PRINTLN ("person run.")); }        StaticString country = "CN";  Public voidspeak () {System.out.println (name+":"+Age ); } person () {name= "Baby"; Age= 1; } person (String N,inta) { age=A; Name=N; }         Public voidSetName (String N) {name=N; }}classConsdemo { Public Static voidMain (string[] args) {person P=NewPerson ();                P.speak (); Person P2=NewPerson ("Wang Choi", 16); P2.setname ("Wang Wang");                P2.speak ();                System.out.println (Person.country);        System.out.println (args);        System.out.println (args.length); Try{System.out.println (args[0]); } Catch(Exception e) {System.out.println ("Log error:" +e); } staticcode.show (); //static classes, class names for method calls    }    }classStaticcode {Static intnum; Static{//Static code blocks, initialization of classesNum= 10; System.out.println ("Static init."); }        Static voidShow () {System.out.println (num); }}

Java Class Basics

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.