Java静态方法的范围 [英] Scope of static methods in Java

查看:41
本文介绍了Java静态方法的范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Java中静态方法的范围是什么?

What is the scope of a static method in Java?

1) (Eclipse) Project level
2) Application level
3) JVM level

如果我们有课程,让您了解我的要求:

To give you insight into what I'm asking, if we have a class:

public class MyClass
{
   private static int data;
   public static void setData(int val)
   {
       data = val;
   }
   public static int getData()
   {
       return data;
   }
}

如果我从另一个类(与 MyClass 处于同一项目中)调用另一个类的 setData()方法,并传递一个值(例如10),我可以从其他项目访问设置的 data (即值10)吗?来自不同的应用程序?等-

And if I make a call to the setData() method from a different class (in the same project as MyClass) and pass a value, say 10, will I be able to access the set data (i.e. value 10) from a different project? from a different application? etc-

或者,我需要做些什么才能能够从另一个(日食)项目访问相同的数据(即10)?

Alternatively, what exactly would I need to do to be able to access the same data (i.e. 10) from a different (eclipse) project?

如果有与此相关的官方文档,请告诉我.

If there is official documentation regarding this, please do let me know.

推荐答案

让我们假设您正在谈论是否存在 data 的一个实例"以及相应的静态方法.

Let's assume you are talking about whether there is one "instance" of data and the corresponding static method.

Java中静态方法的范围是什么?

What is the scope of a static method in Java?

1)(Eclipse)项目级别

1) (Eclipse) Project level

不.源代码/构建时间项目"结构与Java程序的运行时行为直接相关.(项目构建会生成一个或多个.class文件,通常捆绑为JAR或WAR或EAR或任何归档文件.运行时行为取决于将这些文件加载​​到JVM中的方式.)

No. Source code / build time "project" structure has direct relevance to the runtime behaviour of a Java program. (The project build produces one or more .class files, typically bundled up as JAR or WAR or EAR or whatever archive files. The runtime behaviour depends on how those files are loaded into a JVM.)

2)应用程序级别3)JVM级别

2) Application level 3) JVM level

是的,也许吧.

实际的存在范围"(正如某人所描述的)取决于类类型的标识.通常情况下,一次将一个类加载到JVM中.这样就为您提供了一个类类型,并且该类型有一个静态变量实例.

The actual "existence scope" (as someone described it) depends on the identity of the class type. Under normal circumstances, a class is loaded into a JVM once. That gives you one class type, and there is one instance of the static variables for that type.

但是,如果您的类加载器的组织方式正确,则可以再次加载该类.这为您提供了一个新的类类型(与上一个类具有相同的名称...,但具有不同的类加载器),并且该类类型具有自己的一组静态变量.

However, if your classloaders are organized in the right way, you can then load the class again. This gives you a new class type (with the same name as the previous one ... but a different classloader), and that class type has its own set of static variable.

使用 java 命令通过其 main 方法运行的应用程序通常只会加载该类一次.(您通常需要在运行时创建一个新的类加载器以对此进行更改.)

An application that is run using the java command via its main method will typically only load the class once. (You'd typically need to create a new classloader at runtime to change this.)

但是,在框架内运行的应用程序(例如,webapps)必须遵守框架所做的任何事情.典型的appserver框架为每个webapp使用单独的类加载器.因此,如果您在多个Web应用程序中包含一个JAR文件并在同一个应用服务器中运行它们,则可能会获得具有相同名称,不同类加载器....和不同静态变量集的多个类类型.

However, applications (e.g. webapps) that are run within frameworks are subject to whatever the framework does. And a typical appserver framework uses a separate classloader for each webapp. So if you include a JAR file in multiple webapps and run them in the same appserver, you are likely to get multiple class types with the same name and different classloaders .... and different static variable sets.

但是它并没有就此结束,因为当您在另一个类(例如, OtherClass )中调用 MyClass.getData()时,所访问的变量取决于哪个 MyClass 键入已绑定到的 OtherClass 代码.而这取决于 OtherClass 类型的类加载器在加载 OtherClass 类时将其绑定到...的方式.

But it doesn't end there, because when you call MyClass.getData() in another class (e.g. OtherClass), the variable that is accessed depends on which MyClass type the OtherClass code has been bound to. And that depends on what the OtherClass type's classloader bound it to ... when it loaded the OtherClass class.

这一切都可能变得相当复杂,但是通常您不必担心.复杂性只有在某事正在做聪明的类加载器"时才会发生,甚至,聪明的事情通常是实现您想要要发生的应用程序"的分离.

This can all get rather complicated, but normally you don't need to worry about it. The complexity only happens when something is doing "clever classloader stuff", and even, then the clever stuff is usually implementing separation of "apps" that you want to happen.

这篇关于Java静态方法的范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆