Java static fields (properties, methods, classes) __java/android

Source: Internet
Author: User
Tags pow static class

If you define a field as static, then there is only one such domain in each class. In contrast, each object has a copy of its own for all instance domains.

For example, suppose you need to give each employee a unique identification code. Here, add an instance domain ID and a static domain NextID to the employee class:

Class employee{...
    private int id;
    private static int nextid = 1;
}

Now, each employee object has a domain of its own ID, but all instances of this class share a NextID domain. In other words, if you have 1000 objects of the employee class,

There are 1000 instance domain IDs, and after either instance uses the SetID () method, the static field of the employee class becomes the set value. However, there is only one static domain NextID.

Static domain NextID exists even without an employee object. it belongs to a class and does not belong to any independent object. Called directly from a static domain through the class name, the instance domain is invoked by an instance of (class) and can also be corroborated.

An example encountered in a project: If you write a class Xdialog inheritance Dialog,xdialog There is a domain variable private list<string> items; every time I want to display a dialog box, it's new Xdialog (); The dialog box then has a copy of the items, and the new n Xdialog has n corresponding copies of the items. But if items are the number of friends of the current user, then the Xdialog dialog should always show the correct on/off friends, if n copies of the items, each time the initialization of the items, can not achieve synchronization purposes, and inefficient. This solves all the problems with the static keyword and simply declares the privatestatic list<string> items, at which point the items are all Xdialog this class, not an implementation. perfect!

Look at the static method again:

A static method is a method that cannot be applied to an object.

For example, Math.pow (x, a) does not use any of the Math objects when it is operating. In other words, there is no implicit argument (this).

You can think of a static method as a method without this parameter. (In a Non-static method, the This parameter represents an implicit parameter to the method.) As a comparison: a call to a Non-static method (this.) Pow ();

The instance domain cannot be accessed in a static method because the static method cannot manipulate the object. However, static methods can access static fields in their own classes.

public static int Getnextid () {return
    nextid;//return static field
}
This method is called by the class name: int n = Employee.getnextid ();

Note: A static domain cannot directly access an instance domain (because it is related to a class rather than an object), but the instance domain can access the static domain directly (because the instance domain is derived from the class).

If Harry is an employee object, you can use Harry.getnextid () instead of Employee.getnextid (). However, this is a very easy way to confuse people. Because the results of the Getnextid method have nothing to do with Harry . It is recommended that you use the class name instead of the object to invoke the static method.

use static methods in the following two situations:

1. When a method does not require access to the state of an object, its required parameters are provided explicitly (for example, Math.pow).

2. when a method only needs to access the static domain of the class (for example, Employee.getnextid).

Next look at the static inner class :

Sometimes, an inner class is used only to hide a class inside another class, and there is no need for the inner class to reference the Outer class object. To do this, you can declare an inner class as static in order to cancel the resulting reference.

The object of the static inner class is exactly the same as all other internal classes except for the reference privilege of the outer class object that generated it.

For example, define static internal class pair:

Class Arrayalg{public
    static class pair{//does not require an internal class to refer to a peripheral object, breaking this reference with static.
      private double A;
      private double second;
      Public Pair (double F, double s) {I
          = f;
          Second = s;
      }
      Public double GetFirst () {
      return a Public double Getsecond () {return
          second;
      }
      public static Pair Minmax (double[] values) {
          double min = double.max_value;
          Double max = Double.min_value;
          for (double v:values) {
              if (min>v) min = v;
              if (max<v) max = v;
          }
          return new Pair (Min,max);
      }
    End Pair
}

Where the arrayalg is the inner class and the pair is the static inner class.
In main main:

Arrayalg.pair p = ArrayAlg.Pair.minmax (d);//d:double Array
System.out.println ("min=" +p.getfirst () + ", max=" +p.getsecond ());

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.