Scope of static methods in Java

吃可爱长大的小学妹 提交于 2021-02-08 10:19:14

问题


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;
   }
}

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-

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.


回答1:


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

What is the scope of a static method in Java?

1) (Eclipse) Project level

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) Application level 3) JVM level

Yes, no, maybe.

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.

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.)

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.

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.




回答2:


Are you asking about the reference scope or the existence scope? A public static method can be accessed by any class which can access the method's class, given standard Java package naming/scoping rules. Note that this implies (as part of the rules) that access is only available to classes in the same JVM.

The existence of the data is scoped to the instance of java.lang.Class that represents the class being loaded (I'm sure there's some fancy term for this). At most this is until the end of execution of the JVM, but a class can, under some circumstances, be "unloaded" earlier.

It should also be noted that more than one copy (java.lang.Class instance) of a given class can be loaded in a given JVM at the same time, and each copy of the class would have its own static variables. However, this would be pretty rare.




回答3:


static methods do not have a special scope. Scope is defined by the use of access specifier. As you have defined the static method as public so it will be visible to all the classes in the same or different package.

If you want to use it in different project, then either you import the source of your current project in the new project or you create the jar of your current project and add it to the classpath of your new project.




回答4:


Not exactly what you asked, but I believe it might help you understand the concept of static methods.

Let say, you have class Foo with static method calculateFoo1() and non-static method calculateFoo2()

Non-static methods can be used only in association with object of this class (class instance). So before you can call it you need to instantiate the object.

Foo f2 = new Foo();
cf.calculateFoo2;

Static method can be called using Class name without instance of this class.

Foo.calculateFoo1();

You are allowed to call static method using instance of this class as well (if you have it). Compiler will understand.




回答5:


You mixed two concepts son. static means you have one instance per class.

Scope is depending on the access modifiers (private, public, protected, default)

If you want to access this method from other eclipse project, you need to add this project to the the project library.

Right click on your new project, select Build Path, then Add to Build Path Go to projects tab, and add your project that has this getDate method.

Now you can access your class MyClass



来源:https://stackoverflow.com/questions/18927856/scope-of-static-methods-in-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!