What is the difference between a static method and

2018-12-31 06:37发布

See the code snippets below:

Code 1

public class A {
    static int add(int i, int j) {
        return(i + j);
    }
}

public class B extends A {
    public static void main(String args[]) {
        short s = 9;
        System.out.println(add(s, 6));
    }
}

Code 2

public class A {
    int add(int i, int j) {
        return(i + j);
    }
}

public class B extends A {
    public static void main(String args[]) {
    A a = new A();
        short s = 9;
        System.out.println(a.add(s, 6));
    }
}

What is the difference between these code snippets? Both output 15 as an answer.

标签: java
13条回答
若你有天会懂
2楼-- · 2018-12-31 07:09

A static method belongs to the class and a non-static method belongs to an object of a class. That is, a non-static method can only be called on an object of a class that it belongs to. A static method can however be called both on the class as well as an object of the class. A static method can access only static members. A non-static method can access both static and non-static members because at the time when the static method is called, the class might not be instantiated (if it is called on the class itself). In the other case, a non-static method can only be called when the class has already been instantiated. A static method is shared by all instances of the class. These are some of the basic differences. I would also like to point out an often ignored difference in this context. Whenever a method is called in C++/Java/C#, an implicit argument (the 'this' reference) is passed along with/without the other parameters. In case of a static method call, the 'this' reference is not passed as static methods belong to a class and hence do not have the 'this' reference.

Reference:Static Vs Non-Static methods

查看更多
与君花间醉酒
3楼-- · 2018-12-31 07:10

If your method is related to the object's characteristics, you should define it as non-static method. Otherwise, you can define your method as static, and you can use it independently from object.

查看更多
只靠听说
4楼-- · 2018-12-31 07:12

Generally

static: no need to create object we can directly call using

ClassName.methodname()

Non Static: we need to create a object like

ClassName obj=new ClassName()
obj.methodname();
查看更多
柔情千种
5楼-- · 2018-12-31 07:14

Sometimes, you want to have variables that are common to all objects. This is accomplished with the static modifier.

i.e. class human - number of heads (1) is static, same for all humans, however human - haircolor is variable for each human.

Notice that static vars can also be used to share information across all instances

查看更多
君临天下
6楼-- · 2018-12-31 07:18

A static method belongs to the class itself and a non-static (aka instance) method belongs to each object that is generated from that class. If your method does something that doesn't depend on the individual characteristics of its class, make it static (it will make the program's footprint smaller). Otherwise, it should be non-static.

Example:

class Foo {
    int i;

    public Foo(int i) { 
       this.i = i;
    }

    public static String method1() {
       return "An example string that doesn't depend on i (an instance variable)";
    }

    public int method2() {
       return this.i + 1; // Depends on i
    }
}

You can call static methods like this: Foo.method1(). If you try that with method2, it will fail. But this will work: Foo bar = new Foo(1); bar.method2();

查看更多
低头抚发
7楼-- · 2018-12-31 07:19

Basic difference is non static members are declared with out using the keyword 'static'

All the static members (both variables and methods) are referred with the help of class name. Hence the static members of class are also called as class reference members or class members..

In order to access the non static members of a class we should create reference variable . reference variable store an object..

查看更多
登录 后发表回答