Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of Java static initialization and enumerated types

2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the knowledge of "Java static initialization and enumeration type example analysis". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Static initialization:

Conclusion: static initialization is performed only once (when an object of this class is generated for the first time, or when static data members belonging to this class are accessed for the first time, even if objects of that class are not generated).

Proof: see code (1) and (2) need to comment one) and result diagram

Class Cup {Cup (int marker) {System.out.println ("Cup (" + marker + "));} void f (int marker) {System.out.println (" f ("+ marker +") ");}} class Cups {static Cup cup1;static Cup cup2;static {cup1 = new Cup (1); cup2 = new Cup (2);} Cups () {System.out.println (" Cpus () ") / / (1) / / Cups cpus1 = new Cups (); / / (2)}} public class Test2 {public static void main (String [] args) {Cups.cup1.f (99);}}

Note (2):

Note (1):

2. Enumerate types:

1. Enumerated types (enum) Overview: enum is not a data type, but a class, and has its own methods. For example, create an enumerated type whose name is not Spiciness, which has five nominal values, and because the instance of the enumerated type is constant, it is conventionally expressed in uppercase letters, and if there are multiple words in a name with a nominal value, it is separated by an underscore.

Enum Spiciness {NOT, MILD, MEDIUM, HOT, FLAMING} public class Test2 {public static void main (String [] args) {Spiciness howHot = Spiciness.MEDIUM;System.out.println (howHot);}}

Output result:

two。 Method properties of enumerated types:

(1) automatically override the toString () method to make it easier to display the name of an enum instance.

(2) automatically create the ordinal () method to display the declaration order of a particular enum constant.

(3) the static values () method is automatically created to generate an array of enum constants in the order in which they are declared.

Enum Spiciness {NOT, MILD, MEDIUM, HOT, FLAMING} public class Test2 {public static void main (String [] args) {for (Spiciness s: Spiciness.values ()) {System.out.println (s + ", ordinal" + s.ordinal ());}

3. Enumerated types are combined with switch:

Because switch has to choose from a limited set of possible values, it and enum are an excellent combination.

Enum Spiciness {NOT, MILD, MEDIUM, HOT, FLAMING} class Burrito {Spiciness degree;public Burrito (Spiciness degree) {this.degree = degree;} public void describe () {System.out.print ("This burrito is"); switch (degree) {case NOT:System.out.println ("not spicy at all."); break;case MILD:System.out.println ("a little hot."); break Case HOT:case FLAMING:default:System.out.println ("maybe too hot");}} public class Test2 {public static void main (String [] args) {Burrito plain = new Burrito (Spiciness.NOT), greenChile = new Burrito (Spiciness.MEDIUM); plain.describe (); greenChile.describe ();}}

Output result:

This is the end of the content of "Java static initialization and enumeration type example analysis". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

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.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report