In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article will explain in detail the example analysis of static and privatization in Java. The editor thinks it is very practical, so I share it with you for reference. I hope you can get something after reading this article.
The details are as follows:
1. Static has two main functions: first, when you want some properties in the class to be shared by all objects, you must declare them as static properties; second, if a method in a class is called by the class name, you can declare it as a static method.
2. Note that methods that are not declared by static can call properties and methods declared by statci, but methods declared by static cannot call properties and methods declared by non-static types.
3. The static method calls the static variable
Public class Pvf {static boolean Paddy; public static void main (String [] args) {System.out.println (Paddy);}}
The output is
False
Analysis: the variable is assigned the default value of false.
4. The static method calls non-static variables.
Public class Sytch {int x = 20; public static void main (String [] args) {System.out.println (x);}}
The output is as follows:
Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static field x
At test02.Sytch.main (Sytch.java:6)
5 、
Public class Sundys {private int court; public static void main (String [] args) {Sundys s = new Sundys (99); System.out.println (s.court);} Sundys (int ballcount) {court = ballcount;}}
The output is as follows:
ninety-nine
Analysis: privatization variables can still be initialized by the constructor.
6. One application of privatization is singleton design pattern.
Class Singleton {private static Singleton instance = new Singleton (); private Singleton () {} public static Singleton getInstance () {return instance;} public void print () {System.out.println ("hello");}} public class SingleDemo05 {public static void main (String [] args) {Singleton S1 = Singleton.getInstance (); Singleton S2 = Singleton.getInstance (); Singleton S3 = Singleton.getInstance (); s1.print (); s2.print () S3.print ();}}
The output is as follows:
Hellohellohello
Analysis: although three Singleton objects are declared, in fact all objects use only instance references, that is, no matter what happens outside, the end result is that only one instantiated object exists. This is the singleton design pattern.
Thus, as long as the constructor is privatized, the generation of instantiated objects can be controlled.
This is the end of the article on "sample Analysis of static and privatization in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.