Java中静态和非静态方法之间的区别

静态方法与非静态方法 (static vs non static methods)

We will study one by one firstly we will start with static methods and ends with non static methods.

我们将逐一研究,首先是静态方法 ,最后是非静态方法

1)静态方法 (1) static methods)

  • We must use static keywords to declare or define static methods.

    我们必须使用静态关键字来声明或定义静态方法。

  • Static methods are associated with the class that means these methods can be called with the class name or with objects or without objects(Direct call) there is no need to declare any objects for static methods.

    静态方法与类相关联,这意味着可以使用类名或对象或不使用对象(直接调用)来调用这些方法,无需为静态方法声明任何对象。

  • Static methods can access only static member and static methods are of the same class or different class that means static method can’t access non-static member or methods.

    静态方法只能访问静态成员,而静态方法属于同一类或不同类,这意味着静态方法不能访问非静态成员或方法。

  • Static methods create only one copy of the whole program and share it with other member or methods.

    静态方法仅创建整个程序的一个副本,并与其他成员或方法共享。

Example:

例:

// Show the behavior of static member and methods  
class StaticClass {
    // declare a static method 
    public static int div(int a, int b) {
        return a / b;
    }
}

public class Main {
    public static void main(String[] args) {
        int p = 10, q = 5;

        // Declare a static member
        String str = "Welcome in Java World";

        // Invoking the static method with the classname
        int div = StaticClass.div(p, q);

        /* 
         Here we are calling static methods directly without 
         objects or classname. If we want to call with classname 
         or with object then also not a problem. 
        */
        System.out.print("The value of str is = " + str);
        System.out.print("The divide of two number is = " + div);
    }
}

Output

输出量

D:\Programs>javac Main.java

D:\Programs>java Main
The value of str is = Welcome in Java World
The divide of two number is = 2

2)非静态方法 (2) Non-Static methods)

  • We must not have static keywords before method name to declare or define static methods.

    在方法名称之前,我们不能包含静态关键字来声明或定义静态方法。

  • Non-static methods are not associated with the class that means these methods cannot be called with the class name and it is mandatory to declare objects and non-static methods can be called with the object name.

    非静态方法与类无关,这意味着不能使用类名调用这些方法,并且必须声明对象,并且可以使用对象名来调用非静态方法。

  • Non-static methods can access static member and static methods and non-static member and non-static methods are of the same class or different class but non-static can’t modify static member or methods values.

    非静态方法可以访问静态成员和静态方法,非静态成员和非静态方法属于同一类或不同类,但非静态不能修改静态成员或方法的值。

  • For non-static methods create an individual copy of all the objects or in other words create a separate copy of all the objects.

    对于非静态方法,创建所有对象的单独副本,或者换句话说,创建所有对象的单独副本。

Example:

例:

class NonstaticClass {
    // Declare a non-static method 
    public int div(int a, int b) {
        return a / b;
    }
}

public class Main {
    public static void main(String[] args) {
        int p = 10, q = 5;

        // Declare an object of NonstaticClass
        NonstaticClass nc = new NonstaticClass();

        int div = nc.div(p, q);

        // Invoking the non-static method with the class object.
        System.out.print("The div of two num is = " + div);
    }
}

Output

输出量

D:\Programs>javac Main.java

D:\Programs>java Main
The div of two num is = 2


翻译自: https://www.includehelp.com/java/differences-between-static-and-non-static-method-in-java.aspx

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值