📜  Java中的静态方法与示例

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

Java中的静态方法与示例

static 关键字用于构造无论是否生成类的任何实例都将存在的方法。任何使用 static 关键字的方法都称为静态方法。

静态方法的特点:

  • Java中的静态方法是属于类的一部分而不是该类的实例的方法。
  • 类的每个实例都可以访问该方法。
  • 静态方法无需使用类的对象(实例)即可访问类变量(静态变量)。
  • 静态方法只能访问静态数据。它无法访问非静态数据(实例变量)。
  • 在静态和非静态方法中,静态方法都可以直接访问。

声明静态方法的语法:

Access_modifier static void methodName()
{ 
     // Method body.
} 

类的名称可用于调用或访问静态方法。

调用静态方法的语法:

className.methodName(); 

示例 1:静态方法无权访问实例变量

JVM首先运行静态方法,然后创建类实例。因为使用静态方法时没有对象是可访问的。静态方法无权访问实例变量。因此,静态方法无法访问类的实例变量。

Java
// Java program to demonstrate that
// The static method does not have
// access to the instance variable
  
import java.io.*;
  
public class GFG {
    // static variable
    static int a = 40;
  
    // instance variable
    int b = 50;
  
    void simpleDisplay()
    {
        System.out.println(a);
        System.out.println(b);
    }
  
    // Declaration of a static method.
    static void staticDisplay()
    { 
      System.out.println(a); 
    }
  
    // main method
    public static void main(String[] args)
    {
        GFG obj = new GFG();
        obj.simpleDisplay();
  
        // Calling static method.
        staticDisplay();
    }
}


Java
// Java program to demonstrate that
// In both static and non-static methods,
// static methods are directly accessed.
  
import java.io.*;
  
public class StaticExample {
    
    static int num = 100;
    static String str = "GeeksForGeeks";
  
    // This is Static method
    static void display()
    {
        System.out.println("static number is " + num);
        System.out.println("static string is " + str);
    }
  
    // non-static method
    void nonstatic()
    {
        // our static method can accessed 
        // in non static method
        display();
    }
  
    // main method
    public static void main(String args[])
    {
        StaticExample obj = new StaticExample();
        
        // This is object to call non static function
        obj.nonstatic();
        
        // static method can called 
        // directly without an object
        display();
    }
}


输出
40
50
40

示例 2:在静态和非静态方法中,静态方法都是直接访问的。

Java

// Java program to demonstrate that
// In both static and non-static methods,
// static methods are directly accessed.
  
import java.io.*;
  
public class StaticExample {
    
    static int num = 100;
    static String str = "GeeksForGeeks";
  
    // This is Static method
    static void display()
    {
        System.out.println("static number is " + num);
        System.out.println("static string is " + str);
    }
  
    // non-static method
    void nonstatic()
    {
        // our static method can accessed 
        // in non static method
        display();
    }
  
    // main method
    public static void main(String args[])
    {
        StaticExample obj = new StaticExample();
        
        // This is object to call non static function
        obj.nonstatic();
        
        // static method can called 
        // directly without an object
        display();
    }
}
输出
static number is 100
static string is GeeksForGeeks
static number is 100
static string is GeeksForGeeks

为什么使用静态方法?

  1. 访问和更改静态变量和其他非基于对象的静态方法。
  2. 实用程序和辅助类经常使用静态方法。

静态方法的限制:

  1. 非静态数据成员或非静态方法不能被静态方法使用,静态方法也不能直接调用非静态方法。
  2. 在静态环境中,不允许使用 this 和 super。

为什么Java中的main方法是静态的?

这是因为对象不需要调用静态方法。如果是非静态函数,JVM会在调用main()方法之前先构建一个对象,造成额外的内存分配困难。

静态方法和实例方法的区别

Instance Methods

Static Methods

It requires an object of the class.It doesn’t require an object of the class.
It can access all attributes of a class.It can access only the static attribute of a class.
The methods can be accessed only using object reference.The method is only accessed by class name.
Syntax: Objref.methodname()Syntax: className.methodname()
It’s an example of pass-by-value programming.It is an example of pass-by-reference programming.