java 静态方法

在使用java的时候,你会发现,有些对象,需要new ,有些则不需要时,比如Math类

int c = Math.abs(2);

如果你查看源码就会大致的发现,里面的属性和方法都是静态的:

    public static double sin(double a) {
        return StrictMath.sin(a); // default impl. delegates to StrictMath
    }

    public static double asin(double a) {
        return StrictMath.asin(a); // default impl. delegates to StrictMath
    }

如果说:实例变量和实例 方法 是属于某个对象的话,

那么:静态变量,和静态方法 就是属于所有对象也就是属于类的,所以不用new 出一个变量就可以使用。

这样的,一般用做java中些工具类的室时候用的比较多。

原文地址:https://www.cnblogs.com/sunxun/p/5354014.html