[JAVA] [Java essays starting from 0] How to use static properties and considerations

Source: Internet
Author: User
Tags static class

In addition to inner classes, classes generally cannot have static properties.

An external class static_test and an internal static class Static_class are defined as follows:

class static_test{        static_test () {        1;    }          Public Static int s_int;          Public Static void S_func () {        System. out. println (""+S_int);}    }

 Public classhello{Hello () {} Public voidSetaintnum) {a=num; }         Public voidSETB (intnum) {b=num; }     Public voidShowa () {System. out. println ("A ="+a); }     Public voidShowb () {System. out. println ("B ="+b); }         Public Static intA;  Public intb; Static classstatic_class{Static_class () {S_int=2; }                 Public Static intS_int;  Public Static voidS_func () {System. out. println ("S_int ="+s_int); }    }     Public Static voidMain (string[] args) {Hello H1=NewHello (); Hello H2=NewHello (); System. out. println ("H1:");        H1.showa (); H1.seta (4); H1.SETB (2);        H1.SHOWB (); System. out. println ("H2:");        H2.showa (); H2.SETB (3);        H2.SHOWB (); System. out. println ("h:"); System. out. println ("A ="+hello.a); Static_test St=Newstatic_test (); Static_class SC=NewStatic_class ();                Static_test.s_func ();    Static_class.s_func (); }}

Output:

When you invoke a static member of an external class and an inner class, you can only invoke it by using the [class name] or [member name], or by using the object to invoke the static member.

Static members are categorized as all, and static members that are called by all instance objects point to static members of the class itself, and if static members are modified, all objects are called with a modified static member.

[JAVA] [Java essays starting from 0] How to use static properties and considerations

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.