Static variables, static methods, static classes in Java

Source: Internet
Author: User

Static variables, static methods, static classes in Java

Static variables and static methods are static objects, and it is necessary to explain the differences between them and non-static objects.

(1) What is the difference between a Java static object and a non-static object?

The comparison is as follows:

                                           Static Objects                                                            non-static objects       owning property:                 is a class of co-owned                                                 is a memory allocation independently owned by the class objects:                is fixed on the memory space                                          Space is allocated within each of the subordinate classesAllocation Order: Allocates the space of the static object and then allocates the space to the non-static object, that is, the initialization order is first static and non-static. What are the benefits of Java static objects?

A, the data of the static object is unique in the whole, and changed. If you want to deal with something that is unique throughout the program, it's a good way to get static.  A non-static thing you modify later only modifies his own data, but does not affect the data of other homogeneous objects. B, easy to quote. use the class name directly. static method name or class name. A static variable name can be referenced and can modify its property values directly, without the get and set methods. C, keep the data unique. This data is unique globally, modifying any of his places, where all the applications are used will be reflected in the changes to the data. Effectively reduce unnecessary waste.

D,static final is used to modify member variables and member methods, which can be simply understood as "global constants." For a variable, the value is not modifiable once it is given, and for a method, it is not overwritten. (2) Static variables, static methods and static blocks

Typically, a class member must be accessed through the object of its class, but it can create a member that can be used by itself without referencing a particular instance. You can create such a member by adding the keyword static before the member's declaration. If a member is declared static, it is able to be accessed before any object of its class is created, without having to refer to any object (regardless of whether the class has static adornments).

You can declare methods and variables as static. The most common example of a static member is main (). Because main () must be called when the program starts executing, it is declared as static. A variable declared as static is essentially a global variable. The method declared as static has the following limitations: •

A, they can only invoke other static methods

B, they can only access static data

C, they cannot refer to this or super in any way (this relates to objects, super is related to inheritance)

Example: If you need to initialize your static variable by calculation, you can declare a static block. The Static block executes only once when the class is loaded. The following example shows a class that has a static method, some static variables, and a static initialization block.

[Java]View Plaincopy
  1. Public class Testnew {
  2. static int a = 3;
  3. static int b;
  4. static void meth (int x) {
  5. System.out.println ("x =" +x);
  6. System.out.println ("a =" +a);
  7. System.out.println ("b =" +b);
  8. }
  9. Static {
  10. System.out.println ("static block initialized");
  11. b = A *4;
  12. }
  13. public static void Main (string[] args) {
  14. //TODO auto-generated method stub
  15. Meth (42);
  16. }
  17. }

The execution results are:

static block initialized x = a = 3 B = 12

The order of execution for class testnew is: First, A is set to 3, then the static block executes (prints a message), and finally B is initialized to a*4 12. Then call Main (), main () call meth () and pass the value 42 to X. The 3 println () statements refer to two static variables A and B, and the local variable x. (3) External use of static variables or static methods

Outside of the classes that define them, static methods and variables can be used independently of any object, as long as you add the number operator after the name of the class. As you can see, this format is similar to calling a non-static method or variable through an object reference variable. This is how Java implements a control version of global functionality and global variables. Example:

[Java]View Plaincopy
  1. Class staticdemo{
  2. static int a = 42;
  3. static int b = 99;
  4. static void CallMe () {
  5. System.out.println ("a =" +a);
  6. }
  7. }
  8. Public class Testnew {
  9. public static void Main (string[] args) {
  10. //TODO auto-generated method stub
  11. Staticdemo.callme ();
  12. System.out.println ("b =" +staticdemo.b);
  13. }
  14. }

Execution Result:

A = 99 B =

(4) A static class refers to the interior of a class and defines a class that is decorated with static. And where does the static class function? It can be understood in the structure of a struct in C, followed by 2 concepts: inner class and Static modifier statics. A, first, the inner class is used because the inner class has a certain relationship with the outer class, and often only the outer class calls this inner class. Therefore, it is not necessary to use a Java file specifically to store this class.

B, Static is used to modify the internal members of the class. such as static methods, static member variables. Its only function is to be generated as the class is loaded (not as the object is produced), so that it can be obtained directly with the class name + static member name. This allows the static inner class to be understood, and it can be obtained directly from the external class name + internal class name. Examples are as follows:

public class Build {

..............

public static class VERSION {

................

public static final String RELEASE = getString ("Ro.build.version.release");

................

}

................

}

The external can be accessed directly through the Build.VERSION.RELEASE.

Add one: the definition of class variables in a class (independent of the static topic). WakeLock is a class in the PowerManager class, has been import Android.os.PowerManager, if you want to define a WakeLock type of variable, you need Powermanager.wakelock Msmartstaywakelock = NULL, if you want to WakeLock msmartstaywakelock = null, import android.os.PowerManager.WakeLock with import;.

Reference Original: http://www.cnblogs.com/-0_0-/articles/1962368.html

Reference Original: http://blog.sina.com.cn/s/blog_5cd7f5b40100r0rt.html

Reference Original: http://zhidao.baidu.com/question/149873207.html

Reference Original: http://ddvcxj.blog.51cto.com/1064441/265347

Static variables, static methods, static classes in Java

Related Article

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.