Multi-line program angle Understanding static Class

Source: Internet
Author: User

Often called a nested class , when an inner class is static, it means:

1 objects of the enclosing class are not required to create objects of nested classes

2 Non-static perimeter class objects cannot be accessed from objects in nested classes (non-static members of external classes cannot be accessed from objects in static inner classes)

You do not need to bind an instance of a static inner class to an instance of an external class when creating a static inner class. The object of a normal non-static inner class is attached to an outer class object, and to define a static inner class within an external class, you do not need to use the keyword new to create an instance of the inner class. Static classes and methods only belong to the class itself, not to objects of that class, or to objects of other external classes, so only static methods of the outer class can be accessed.

Import Java.util.concurrent.countdownlatch;import Java.util.concurrent.countdownlatch;public class Innerstaticclass {public static volatile int n = 0;//public integer m = 0;final integer lock = new integer (1); static Clas S MyThread1 implements Runnable {public static Integer LocalLock = new Integer (2);p ublic static int value = 2;public Stati C synchronized Void Inc () {n++;} public void Run () {for (int i = 0; i < i++) try {System.out.println (this);//Inc (); method One//Method two//synchronized (lock ) {//n = n + 1;//}//method three//synchronized (this) {//n = n + 1;//}//method four synchronized (locallock) {n = n + 1;} Thread.Sleep (3);} catch (Exception e) {}}}public void Runthread () throws interruptedexception {Long beg = System.currenttimemillis (); mythread1[] mythreadary = new mythread1[100]; thread[] Threadary = new Thread[100];for (int i = 0; i < threadary.length; i++) {Mythreadary[i] = new MyThread1 ();//Sy Stem.out.println (mythreadary[i].value); mythreadary[i].value = 32;threadary[i] = new Thread (MythreaDary[i], "test1");} for (int i = 0; i < threadary.length; i++) {Threadary[i].start ();} for (int i = 0; i < threadary.length; i++) {threadary[i].join ();//System.out.println ((Mythreadary[i]). value);} System.out.println (INNERSTATICCLASS.N); System.out.println (m); SYSTEM.OUT.PRINTLN ("The program Takes time:" + (System.currenttimemillis ()-beg));} public static void Main (string[] args) throws interruptedexception {innerstaticclass tmp = new Innerstaticclass (); Tmp.run Thread ();}}

  

1 set a non-static method of nested classes, and you can see that each object is independent of each other, as with non-static methods

2 Synchronized method, because we need to synchronize the data is N, belongs to the static class outside, synchronized must be used in its static method, is equal to all the objects of the class synchronization (method one), or used in the code block, the object must use the external variable (method two), method three uses the Is this, the class object itself, the problem occurs when synchronizing external data, which is used only for the same static class object to synchronize data between different threads

3 The difference between a static class and a normal class is found by testing

1 You can create a static class without creating an external object, it does not have a dependency on any object, and only has dependencies on itself, so if you use the Access class outside the member must be a static member, the non-static member must use the object. Member to access the operation

2 static variables can be used internally, and if you use a synchronous method static variable, the entire class is synchronized, and if method four, multiple static class objects are executed synchronously on multiple threads

Multi-line program angle Understanding static Class

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.