静态方法和变量

发布于 2024-12-23 01:56:23 字数 95 浏览 0 评论 0原文

我确实知道在java中静态方法只能使用静态变量和静态方法,但非静态方法可以使用非静态变量和方法。有什么解释为什么静态方法只能访问静态变量/方法吗?并且无法访问非静态方法和变量?

I do know that in java static methods can only use static variables and static methods and but non static methods can use non static variables and methods. is there any explanation why static methods can only access static variables/methods? and cannot access non static methods and variables?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

∞觅青森が 2024-12-30 01:56:23

要访问非静态方法/字段,您需要该类的实例。

to access non-static methods/fields, you require an instance of the class.

若能看破又如何 2024-12-30 01:56:23

当您实例化一个类时,它将被加载到堆或堆栈中。在这种情况下,您的应用程序引用了内存实例所在的那些部分。然而,静态方法只是加载到内存中的函数。这就是为什么如果您的静态方法想要访问类变量或方法,它不能只是调用它,因为堆中可能存在对该类的多个引用,而不是一个且只能存在一个静态方法。

如果您想从静态方法调用任何非静态方法,您应该使用该类的实例(或创建一个实例),以便您可以定义您真正想要的类引用。

When you instantiate a class, it will be loaded into a heap or stack. In this case your application has references to those parts of memory instances are located. However, static methods are just functions loaded into the memory. That is why if your static method wants to access a class variable or method it cannot just call it because there may be multiple references to that class in the heap contrary to one and only one static method can exist.

If you want to call any non-static method from a static method, you should use the instance (or create one) to that class so that you can define which class reference you really mean.

菩提树下叶撕阳。 2024-12-30 01:56:23

想一想:

a) 假设你是一个静态方法。

b) 你独立于任何对象实例而存在。即使没有类实例,你也存在。

c) 您想要访问非静态变量。存在于一个且仅一个对象实例中的变量 - 特定于该特定对象实例的变量。

d) 您要使用哪个对象实例的非静态变量???

静态方法可以访问类的非静态成员...前提是它们通过类引用来执行此操作。

否则 - 没有类引用 - 无法访问。

有道理吗? ;)

Think about it:

a) Suppose you were a static method.

b) You exist independently of any object instance. You exist even if there's NO class instance.

c) You want to access a non-static variable. A variable that exists in ONE and ONLY one object instance - a variable that's SPECIFIC to THAT PARTICULAR object instance.

d) Which object instance's non-static variable are you going to use???

Static methods MAY access non-static members of classes... provided they do it through a class reference.

Otherwise - no class reference - no access.

Make sense? ;)

小猫一只 2024-12-30 01:56:23

在静态上下文中,“this”关键字不可访问。我们不能在静态上下文中使用此关键字。实例变量在构造函数调用时初始化。但静态变量是在类加载时初始化的。在类加载期间,实例变量在这段时间内仍未被识别,如果我们在静态上下文中使用实例变量,则会导致编译时错误,因为当时它们未被识别。

In the Static context 'this' keyword is not accessible . we cannot use this keyword in the static context . Instance variables are initialized when the constructor invokes. But static variables are initialized when the class loads. during the loading of the class , instance variables are still not identified during this time if we use instance variables in static context it leads to compile time error because they are not identified at that time

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文