In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces how to achieve declaration and initialization in java, the content is very detailed, interested friends can refer to, hope to be helpful to you.
Initialization sequence
According to the definition in JLS (Java Language Specification), class needs to initialize both the static initialization program defined in class and the initialization program of static fields (class variables) declared in this class during the initialization process.
For the static variable, if the static variable is defined as final and its value is the compile time value, then the static variable will be initialized first.
So with the use of the final static variable, is there no initialization problem?
Let's look at the following example:
Public class StaticFiledOrder {private final int result; private static final StaticFiledOrder instance = new StaticFiledOrder (); private static final int intValue=100; public StaticFiledOrder () {result= intValue-10;} public static void main (String [] args) {System.out.println (instance.result);}}
What is the output?
The answer is 90. According to the rules we mentioned, intValue is final and constant is assigned when it is compiled, so it is initialized first. Instance calls the constructor of the StaticFiledOrder class, resulting in a value of 90 for result.
Next, let's change the intValue to a random variable:
Public class StaticFiledOrder {private final int result; private static final StaticFiledOrder instance = new StaticFiledOrder (); private static final int intValue= (int) Math.random () * 1000; public StaticFiledOrder () {result= intValue-10;} public static void main (String [] args) {System.out.println (instance.result);}}
What is the result of the operation?
The answer is-10. Why?
Because intValue has not been initialized when instance calls the StaticFiledOrder constructor for initialization, it has a default value of 0, resulting in a final value of-10 for result.
How do I modify it?
Just change the order:
Public class StaticFiledOrder {private final int result; private static final int intValue= (int) Math.random () * 1000; private static final StaticFiledOrder instance = new StaticFiledOrder (); public StaticFiledOrder () {result= intValue-10;} public static void main (String [] args) {System.out.println (instance.result);}} cycle initialization
Now that the static variable can call the constructor, is it possible to call methods of other classes?
Take a look at this example:
Public class CycleClassA {public static final int a = CycleClassB.b+1;} public class CycleClassB {public static final int b = CycleClassA.a+1;}
The above is an example of loop initialization. In the above example, an in CycleClassA refers to CycleClassB's b, while b in the same CycleClassB refers to CycleClassA's a.
This circular reference will not report an error, but depending on the initialization order of the class, it will cause an and b to produce two different results.
So in the process of writing code, we must avoid this kind of loop initialization.
Do not use the class name in the java standard library as your own class name
Many excellent classes are defined for us in the java standard library, and we can easily use them when building our own java programs.
But when writing custom classes, we must be careful to avoid using the same names as those in the java standard library.
This should be easy to understand, just to avoid confusion. So as not to cause unnecessary accidents.
This is very simple, so I won't give an example.
Do not change the value of the quantity in the enhanced for statement
As we traverse collections and arrays, in addition to the original for statements, java provides us with the following enhanced for loops:
For (I # I = Expression.iterator (); # i.hasNext ();) {{VariableModifier} TargetType Identifier = (TargetType) # i.next (); Statement}
In the process of traversal, # I is actually equivalent to a local variable, and changes to this local variable will not affect the collection itself.
Let's look at an example:
Public void noncompliantUsage () {int [] intArray = new int [] {1pc2, 3pje, 5pc6}; for (int I: intArray) {ionomo;} for (int I: intArray) {System.out.println (I);}}
During the traversal, we tried to set the I to 0, but finally output the result of intArray and found that there was no change.
So, generally speaking, we need to set # I to final in the enhanced for statement to eliminate this unnecessary logical misunderstanding.
Public void compliantUsage () {int [] intArray = new int [] {1 for 2 intArray = new int 6}; for (final int i: intArray) {} for (int I: intArray) {System.out.println (I);}} about how to implement declaration and initialization in java is shared here. I hope the above content can be helpful to you and learn more knowledge. If you think the article is good, you can 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.