📜  Java中的静态方法与实例方法

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

Java中的静态方法与实例方法

实例方法

实例方法是需要在调用它之前创建其类的对象的方法。要调用实例方法,我们必须创建定义该方法的类的对象。

public void geek(String name)
{
 // code to be executed....
}
// Return type can be int, float String or user defined data type.

内存分配:这些方法本身存储在堆的永久代空间中,但参数(传递给它们的参数)及其局部变量和要返回的值是在堆栈中分配的。它们可以在它们所在的同一个类中调用,也可以从同一个包或其他包中定义的不同类中调用,具体取决于提供给所需实例方法的访问类型
要点:

  • 实例方法属于类的对象,而不是类,即它们可以在创建类的对象后调用。
  • 实例方法不是按实例存储的,即使使用虚拟方法也是如此。它们存储在单个内存位置,它们只“知道”它们属于哪个对象,因为当您调用它们时会传递此指针。
  • 它们可以被覆盖,因为它们是在运行时使用动态绑定解决的。

下面是访问实例方法的实现:

Java
// Example to illustrate accessing the instance method .
import java.io.*;
 
class Foo {
 
    String name = "";
 
    // Instance method to be called within the
    // same class or from a another class defined
    // in the same package or in different package.
    public void geek(String name) { this.name = name; }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // create an instance of the class.
        Foo ob = new Foo();
 
        // calling an instance method in the class 'Foo'.
        ob.geek("GeeksforGeeks");
        System.out.println(ob.name);
    }
}


Java
// Example to illustrate Accessing
// the Static method(s) of the class.
import java.io.*;
 
class Geek {
 
    public static String geekName = "";
 
    public static void geek(String name)
    {
        geekName = name;
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // Accessing the static method geek()
        // and field by class name itself.
        Geek.geek("vaibhav");
        System.out.println(Geek.geekName);
 
        // Accessing the static method geek()
        // by using Object's reference.
        Geek obj = new Geek();
        obj.geek("mohit");
        System.out.println(obj.geekName);
    }
}


输出
GeeksforGeeks

静态方法

静态方法是Java中无需创建类对象即可调用的方法。它们由类名本身或对该类的 Object 的引用来引用。

public static void geek(String name)
{
 // code to be executed....
}

// Must have static modifier in their declaration.
// Return type can be int, float, String or user defined data type.

内存分配:

它们存储在堆的永久代空间中,因为它们与它们所在的类相关联,而不是与该类的对象相关联。但是它们的局部变量和传递给它们的参数存储在堆栈中。由于它们属于类,因此可以在不创建类的对象的情况下调用它们。

要点:

  • 静态方法与它们所在的类相关联,即在不创建类实例的情况下调用它们,即ClassName.methodName(args)
  • 它们的设计目的是在从同一类创建的所有对象之间共享。
  • 静态方法不能被覆盖,因为它们是由编译器在编译时使用静态绑定解析的。但是,我们可以在父类子类中声明同名的静态方法,但是它将被称为方法隐藏,因为派生类方法将隐藏基类方法。

下面是访问静态方法的说明:

Java

// Example to illustrate Accessing
// the Static method(s) of the class.
import java.io.*;
 
class Geek {
 
    public static String geekName = "";
 
    public static void geek(String name)
    {
        geekName = name;
    }
}
 
class GFG {
    public static void main(String[] args)
    {
 
        // Accessing the static method geek()
        // and field by class name itself.
        Geek.geek("vaibhav");
        System.out.println(Geek.geekName);
 
        // Accessing the static method geek()
        // by using Object's reference.
        Geek obj = new Geek();
        obj.geek("mohit");
        System.out.println(obj.geekName);
    }
}
输出
vaibhav
mohit

注意:类中定义的静态变量及其值(基元或引用)存储在PermGen内存空间中。

如果静态变量引用一个对象怎么办?

static int i = 1;
static Object obj = new Object();

在第一行中,值 1 将存储在 PermGen 部分中。在第二行中,引用 obj 将存储在 PermGen 部分中,而它所引用的 Object 将存储在堆部分中。

什么时候使用静态方法?

  • 当您拥有可以在同一类的所有实例之间共享的代码时,请将这部分代码放入静态方法中。
  • 它们基本上用于访问类的静态字段。

实例方法与静态方法

  • 实例方法可以直接访问实例方法和实例变量。
  • 实例方法可以直接访问静态变量和静态方法。
  • 静态方法可以直接访问静态变量和静态方法。
  • 静态方法不能直接访问实例方法和实例变量。他们必须使用对对象的引用。并且静态方法不能使用 this 关键字,因为没有“this”可以引用的实例。

参考

  • https://docs.oracle.com/javase/tutorial/java Java
  • 堆栈溢出