简体   繁体   English

是否可以通过类方法访问实例方法和变量

[英]is it possible to access instance methods and variable via class methods

Till I read this on Oracle Doc (Class methods cannot access instance variables or instance methods directly—they must use an object reference) the only thing I know, about instance methods and variables are not able to be accessed by the class(static) methods directly. 直到我在Oracle Doc上读到这个(类方法不能直接访问实例变量或实例方法 - 它们必须使用对象引用)我唯一知道的,关于实例方法和变量不能被类(静态)方法访问直。

What does it mean when it says ....they must use an object reference? 当它说......他们必须使用对象参考时意味着什么? Does it mean we can access instance variables and methods indirectly using class methods? 这是否意味着我们可以使用类方法间接访问实例变量和方法?

Thank you in advance. 先感谢您。

It means that this is allowed: 这意味着允许这样做:

public class Test {
    public int instanceVariable = 42;
    public void instanceMethod() {System.out.println("Hello!");}

    public static void staticMethod() {
        Test test = new Test();

        System.out.println(test.instanceVariable); // prints 42
        test.instanceMethod(); // prints Hello!
    }
}

and this is not: 这不是:

public class Test {
    public int instanceVariable = 42;
    public void instanceMethod() {System.out.println("Hello!");}

    public static void staticMethod() {
        System.out.println(instanceVariable); // compilation error
        instanceMethod(); // compilation error
    }
}

An instance variable, as the name suggests is tied to an instance of a class. 顾名思义,实例变量与类的实例相关联。 Therefore, accessing it directly from a class method, which is not tied to any specific instance doesn't make sense. 因此,直接从类方法访问它,这与任何特定实例无关,这没有意义。 Therefore, to access the instance variable, you must have an instance of the class from which you access the instance variable. 因此,要访问实例变量,您必须具有从中访问实例变量的类的实例。

The reverse is not true however - a class variable is at the "top level", and so is accessible to instance methods and variables. 但事实并非如此 - 类变量位于“顶级”,因此实例方法和变量可以访问。

class MyClass;
{  
 public int x = 2;
 public static int y = 2;

 private int z = y - 1; //This will compile.

 public static void main(String args[])
 {
    System.out.println("Hello, World!");
 }

 public static void show()
 {
    System.out.println(x + y); // x is for an instance, y is not! This will not compile.

    MyClass m = new MyClass();
    System.out.println(m.x + y); // x is for the instance m, so this will compile.
 }

 public void show2()
 {
  System.out.println(x + y); // x is per instance, y is for the class but accessible to every instance, so this will compile.
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 实例方法是否需要访问 java 中的实例变量? - Is it necessary for instance methods to access instance variable in java? Class 方法与实例方法 - Class methods vs instance methods 无法通过泛型访问类方法 - Cannot access class methods via generics 是否可以在接口类中访问通用类型的方法? - Is it possible to access methods of generic type in an interface class? 接口类型的实例变量无法访问实现类方法,而类型实现类的实例变量可以访问 - Instance variable of interface type does not have access to implementation class methods where as instance variable of type implementation class do 是否可以创建一个“超级”变量,然后将其更改为子 class 类型并能够访问子类方法 - Is it possible to Create a 'super' variable and then change it to a sub class type and be able to access the subclass methods Java无法从类实例访问类方法 - Java can't access class methods from class instance 无法通过子 class 访问父 class 方法 - Not able to access the parent class methods via child class 如何通过包装类访问direcly类成员的方法 - how to access direcly class member's methods via wrapper class Java如何使用另一个类的实例访问方法 - Java how to access methods using an instance from another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM