📜  Java中静态方法和非静态方法的区别

📅  最后修改于: 2022-05-13 01:54:44.324000             🧑  作者: Mango

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

静态方法是属于某个类的方法,但它不属于该类的实例,并且可以在没有该类的实例或对象的情况下调用该方法。 Java中的每个方法都默认为非静态方法,前面没有static关键字。非静态方法可以访问任何静态方法和静态变量,而无需创建对象的实例。让我们澄清差异以下是这些指针之间的各种重要差异,如下所示:

  1. 访问成员和方法
  2. 调用过程
  3. 装订过程
  4. 覆盖过程
  5. 内存分配

#1:访问成员和方法

静态方法只能访问另一个类或同一个类的静态数据成员和静态方法,而不能访问非静态方法和变量。此外,静态方法可以重写任何静态数据成员的值。

非静态方法可以访问静态数据成员和静态方法以及另一个类或同一个类的非静态成员和方法,也可以更改任何静态数据成员的值

例子

Java
// Java program to Illustrate Calling of a Static Method
 
// Class 1
// Helper class
class Helper {
 
    // Static method
    public static int sum(int a, int b)
    {
        // Simply returning the sum
        return a + b;
    }
}
 
// Class 2
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Custom values for integer
        // to be summed up
        int n = 3, m = 6;
 
        // Calling the static method of above class
        // and storing sum in integer variable
        int s = Helper.sum(n, m);
 
        // Print and display the sum
        System.out.print("sum is = " + s);
    }
}


Java
// Java program to Illustrate Calling of a Non-Static Method
 
// Class 1
// Helper class
class Helper {
 
    // Non-static method
    public int sum(int a, int b)
    {
        // Returning sum of numbers
        return a + b;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Input integers to be summed up
        int n = 3, m = 6;
 
        // Creating object of above class
        Helper g = new Helper();
 
        // Calling above method to compute sum
        int s = g.sum(n, m);
 
        // Calling the non-static method
        System.out.print("sum is = " + s);
    }
}


Java
// Override of static method
class Parent {
 
    // static method
    static void show()
    {
        System.out.println("Parent");
    }
}
 
// Parent inherit in Child class
class Child extends Parent {
 
    // override show() of Parent
    void show()
    {
        System.out.println("Child");
    }
}
 
class GFG {
    public static void main(String[] args)
    {
        Parent p = new Parent();
        // calling Parent's show()
        p.show();
        // cannot override Parent's show()
    }
}


Java
// Override of non-static method
 
class Parent {
    void show()
    {
        System.out.println("Parent");
    }
}
 
// Parent inherit in Child class
class Child extends Parent {
 
    // override show() of Parent
    void show()
    {
        System.out.println("Child");
    }
}
 
class GFG {
    public static void main(String[] args)
    {
        Parent p = new Parent();
        // calling Parent's show()
        p.show();
 
        Parent c = new Child();
        // calling Child's show()
        c.show();
    }
}


输出
sum is = 9

例子

Java

// Java program to Illustrate Calling of a Non-Static Method
 
// Class 1
// Helper class
class Helper {
 
    // Non-static method
    public int sum(int a, int b)
    {
        // Returning sum of numbers
        return a + b;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Input integers to be summed up
        int n = 3, m = 6;
 
        // Creating object of above class
        Helper g = new Helper();
 
        // Calling above method to compute sum
        int s = g.sum(n, m);
 
        // Calling the non-static method
        System.out.print("sum is = " + s);
    }
}
输出
sum is = 9

#2:调用过程

静态方法的内存是固定在内存中的,因此我们不需要定义静态方法的类的对象来调用静态方法。要调用该方法,我们需要编写类名,后跟方法名

语法:调用静态方法

class GFG{
 public static void geek()
 { }
}

// calling
GFG.geek();

非静态方法的内存不是固定在ram中的,所以我们需要一个类对象来调用非静态方法。要调用该方法,我们需要编写方法名称,后跟类对象名称

语法:调用非静态方法

class GFG{
 public void geek()
 { }
}

// creating object
GFG g = new GFG();

g.geek();

// calling

#3:绑定过程

静态方法中,该方法使用编译时或早期绑定。因此,我们可以在不创建实例的情况下访问静态方法。在非静态方法中,该方法使用运行时或动态绑定。这样我们就无法在不创建实例的情况下访问非静态方法。

#4:覆盖

静态方法中,由于早期绑定,我们不能覆盖静态方法。

例子:

Java

// Override of static method
class Parent {
 
    // static method
    static void show()
    {
        System.out.println("Parent");
    }
}
 
// Parent inherit in Child class
class Child extends Parent {
 
    // override show() of Parent
    void show()
    {
        System.out.println("Child");
    }
}
 
class GFG {
    public static void main(String[] args)
    {
        Parent p = new Parent();
        // calling Parent's show()
        p.show();
        // cannot override Parent's show()
    }
}

输出:

java:15: error: show() in Child cannot override show() in Parent
    void show()
         ^
  overridden method is static

非静态方法中,我们可以重写非静态方法。因为对于覆盖,我们需要运行时多态性,这仅发生在运行时绑定中。

例子:

Java

// Override of non-static method
 
class Parent {
    void show()
    {
        System.out.println("Parent");
    }
}
 
// Parent inherit in Child class
class Child extends Parent {
 
    // override show() of Parent
    void show()
    {
        System.out.println("Child");
    }
}
 
class GFG {
    public static void main(String[] args)
    {
        Parent p = new Parent();
        // calling Parent's show()
        p.show();
 
        Parent c = new Child();
        // calling Child's show()
        c.show();
    }
}

输出:错误

Parent
Child

#5:内存分配

静态方法中,内存分配只发生一次,因为 static 关键字在 ram 中为该方法固定了一个特定的内存。因此,当每次在程序中调用该方法时,每次都会使用该特定内存。因此,分配的内存更少。

非静态方法中,这里的内存分配是在调用方法时发生的,并且每次调用方法时都会分配内存。这里使用了很多内存。现在,最后绘制表格以便完全掌握

PointsStatic methodNon-static method
DefinitionA static method is a method that belongs to a class, but it does not belong to an instance of that class and this method can be called without the instance or object of that class.Every method in java defaults to a non-static method without a static keyword preceding it. non-static methods can access any static method and static variable also, without using the object of the class.
Accessing members and methodsIn the static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.In the non-static method, the method can access static data members and static methods as well as non-static members and methods of another class or same class. 
 
Binding processThe static method uses compile-time or early binding.The non-static method uses runtime or dynamic binding.
OverridingThe static method cannot be overridden because of early binding.The non-static method can be overridden because of runtime binding.
Memory allocationIn the static method, less memory is used for execution because memory allocation happens only once because the static keyword fixed a particular memory for that method in ram.
 
In the non-static method, much memory is used for execution because here memory allocation happens when the method is invoked and the memory is allocated every time when the method is called.