Java static 关键字
实例
static 方法可以在不先创建类的对象的情况下访问:
public class Main {// Static methodstatic void myStaticMethod() {System.out.println("Static methods can be called without creating objects");}// Public methodpublic void myPublicMethod() {System.out.println("Public methods must be called by creating objects");}// Main methodpublic static void main(String[] args) {myStaticMethod(); // Call the static methodMain myObj = new Main(); // Create an object of MyClassmyObj.myPublicMethod(); // Call the public method}}
定义与用法
static 关键字是用于方法和属性的非访问修饰符。无需创建类的对象,就可以访问静态方法/属性。
关联页面
可以访问本站的 Java 修饰符 学习更多相关知识。