Java static fields and static methods

Java was modified static field or method often referred to as static, then what is it static? Here we take a look at the static fields and static methods in Java.

1, static fields

     If the static field is defined as static, then each is only one such class field, and each object for all instances domain has its own copy of this. For example, students in a class instance field and a static field nextId studentId, as follows:

class Student {
    private static int nextId = 1;
    private String studentId;
    ......
}

         Then, each student objects, will have their own studentId, but it is the object of this class will share a nextId domain, in other words: If there are 100 Student class object, then there are 100 instances domain studentId, each student will have their share, but only a static field nextId, 100 students on this subject static field nextId, so that no student objects, static fields also exist, it belongs to the class, rather than belonging to any independent objects. Static fields (static methods) can be invoked directly by the class name, rather than create an object using object to call (in the language of the vast majority of object-oriented, static fields are referred to as class field. The term "static" just follows the C ++ it is called, there is no practical significance).

        A following implement the setId () method in class Student:

public void setId() {
    studentId = nextId + "";
    nextId++;
}

         The object is assumed to student1 studnetId namely: student1.setId (); studentId then studnet1 instance field is set to the current value of the static field nextId, and the value of the static field nextId were increment operator. This effect is equivalent to:

  student1.id = Student.nextId;

  Student.nextId++;

2, static constants

           Static variables used is relatively small, but they use static constants are more (in the actual development, can do a static constant: the role of change and change a whole at modifications and greatly reduce the wrong place), for example, Java in the Math class defines a static constant pi:

public class Math{
    ...
    public static final double PI = 3.14159265358979323846;
    ...
}

         When in use, we can now use static variables, static methods, like using the static constant (static static can all be modified directly by the class name to use) Math.PI to use this constant.

        If the static keyword is omitted, it becomes a PI Math class instance field, need to access PI by Math class objects, and each object has its own Math a PI copies.

        In addition a multiple use of static constants in Java System.out it is declared in the System class:

public class System {
    ...
    public static final PrintStream = ...;
    ...
}

       We all know that every class object can be modified to the public domain, so it is best not to be the domain design public, however, public constants (that is, final fields), but there is no problem, because out is declared final, so do not allow then other print stream assigned to it:

         System.out = new PrintStream (...); // this is the wrong approach

Note: View the System class, you will find a setOut method, it can be set to different System.out stream, then we might wonder why this method can change the value of the final variable, because, setOut method is a local approach, rather than implemented in the Java language (use the C language), local ways to get around access control mechanisms of the Java language, this is a special approach in the preparation of the program, we should not this treatment, but some developers in the android development is to write native methods, and then using C language, amassing a Java environment call C language here is not to do too much I explained.

3, static methods

           The method is a static method by the object can not be operated, for example, obtains the power of a number of pow () method is a static method Math class. Expression is: Math.pow (x, a), during operation, does not use any Math object, in other words, there is no implicit parameter. It can be considered static method is no argument this way (in a non-static method, this parameter indicates the implicit argument of this method).

        Student class static methods can not access the instance fields Id, Id because it is not static, it can not be static method Student's operation target, but the method can access a static class static field itself. In short, is often said: static method can only be called a static thing, not static can not be used.

public static int getNextId {
    return nextId;  //  返回静态域
}

        This method is called by the class name Student.getNextId (), then this method can omit the static keyword do? The answer is yes, but need to call this method through an object of the Student class.

Use static methods in the following two cases:

1, a method does not need to access the object state, the required parameters are provided by the display parameters (e.g.: Math.pow ()).

2, a domain access method only requires static class (e.g.: Student.getNextId ()).

4, Factory Method

    Another common method for static use, for example: using the NumberFormat class factory methods produce different styles of format object:

NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance();
NumberFormat percentFormatter = NumberFormat.getPercentInstance();
doublen n = 0.3;
System.out.println(currencyFormatter.format(n)); // 输出 ¥0.30
System.out.println(percentFormatter.format(n));  //输出 30%

       Why not use the NumberFormat class constructor completion of these operations it? There are two main reasons:

1, can not be named constructor. Constructor class name must be the same name, however, instances where desired currency and percentages obtained using different instance names.

2, when a constructor, can not change the type of object configuration. And the method will return a DecimalFormat Factory class object, which is a subclass of NumberFormat.

 

5, main method

        When we learn java, the program mostly have a main method, we have a program called the entrance, main method does not operate any object, in fact, when you start the program, there is no object, static main method the implementation of the program and create the required objects.

Published 52 original articles · won praise 96 · views 10000 +

Guess you like

Origin blog.csdn.net/weixin_44170221/article/details/104452903