This article is participating in the Java Theme Month – Java Debug Notes Event, see the event link for details
“Static classes in Java
Is there anything like static class in Java?
What does this class mean? Do all methods of static classes need to be static?
Should it be reversed? If a class contains all static methods, should that class also be static?
What are static classes useful for?
Answer:
Java has statically nested classes, but it sounds like you’re looking for top-level static classes. Java cannot make a top-level class static, but you can emulate static classes like this:
Declare your class final- prevents extending classes, because extending static classes makes no sense
Create the constructor private- to prevent instantiation through client code, because instantiating a static class makes no sense
Creates all members of the class and the function static- cannot call any instance methods or access instance fields because the class cannot be instantiated
Note that the compiler does not prevent you from declaring instance (non-static) members. The problem is shown only if you try to invoke an instance member
A simple example of each suggestion is as follows:
public class TestMyStaticClass { public static void main(String []args){ MyStaticClass.setMyStaticMember(5); System.out.println("Static value: " + MyStaticClass.getMyStaticMember()); System.out.println("Value squared: " + MyStaticClass.squareMyStaticMember()); // MyStaticClass x = new MyStaticClass(); // results in compile time error } } // A top-level Java class mimicking static class behavior public final class MyStaticClass { private MyStaticClass () { // private constructor myStaticMember = 1; } private static int myStaticMember; public static void setMyStaticMember(int val) { myStaticMember = val; } public static int getMyStaticMember() { return myStaticMember; } public static int squareMyStaticMember() { return myStaticMember * myStaticMember; }}Copy the code
What is the use of static classes? A good use of static classes is to define disposable, utility and/or library classes in which instantiation makes no sense. A good example is the Math class, which contains some mathematical constants, such as PI and E, and provides only mathematical calculations. In this case, the need to instantiate would be unnecessary and confusing. See the Math class and source code. Note that it is final and all its members are static. If Java allows top-level classes to be declared static, then the Math class is indeed static.
Answer:
Well, Java has “statically nested classes”, but if you’re from anywhere, they’re nothing like C#’s static classes. A statically nested class is simply a class that has no implicit reference to an external class instance.
Statically nested classes can have instance methods as well as static methods.
There are no top-level static classes in Java.
The article translated from yl2gl72eozkinivz3vc6swkesy – ac4c6men2g7xr2a – translate. Translate. Goog/questions / 7…
It is true that most static classes are utility classes, but we have also written static inner classes to implement thread-safe singletons, with JDK source code for url template classes singletons.
In addition to the static inner classes mentioned above, there is a ThreadPoolExecutor that everyone is familiar with
public class ThreadPoolExecutor extends AbstractExecutorService
Copy the code
It has an inner class, Worker, that inherits AQS
/ / the main attribute is task firstTask and thread thread private final class Worker extends AbstractQueuedSynchronizer implements RunnableCopy the code
It also has a static inner class-reject policy
Public static class CallerRunsPolicy Implements RejectedExecutionHandler Public static Class AbortPolicy implements RejectedExecutionHandler Exception public static class DiscardPolicy implements RejectedExecutionHandler Discard public static Class DiscardOldestPolicy Implements RejectedExecutionHandler Discards the oldest oneCopy the code
ThreadPoolExecutor itself inherits an abstract executor (main methods: add and submit tasks). AQS are used if you want to avoid contention between threads, for example, when executing a task:
final void runWorker(Worker w) { Thread wt = Thread.currentThread(); Runnable task = w.firstTask; w.firstTask = null; w.unlock(); // Release resources exclusivelyCopy the code
unlock
public void unlock() { release(1); }
Copy the code
This public method release of the AQS class
public final boolean release(int arg) { if (tryRelease(arg)) { Node h = head; if (h ! = null && h.waitStatus ! = 0) unparkSuccessor(h); return true; } return false; }Copy the code
Thread pool is used to load, set up automatic collection, for thread pool tuning and principle, see my article:
zhuanlan.zhihu.com/p/147583619
Thank you for reading this, if this article is well written and if you feel there is something to it
Ask for a thumbs up 👍 ask for attention ❤️ ask for share 👥 for 8 abs I really very useful!!
If there are any mistakes in this blog, please comment, thank you very much! ❤ ️ ❤ ️ ❤ ️ ❤ ️