English 中文(简体)
Problem understanding how a non-static method is called
原标题:

I m reading the Java code of libreoffice and there s one thing I don t really understand. The method updateUI() calls 2 time the method updateUI and they pass as a parameter a Vector variable, BUT this method doesn t accept any parameter. Anybody can explain me what this parameter does ? if updateUI() is called this way:

UnoDataAware.updateUI(); 

it complains and says:

non-static method cannot be referenced from a static context

问题回答

There is another method in the second link you provided: (line 203)

 public static void updateUI(Collection dataAwares) {
            for (Iterator i = dataAwares.iterator(); i.hasNext();)
                ((DataAware) i.next()).updateUI();
         }

This is method that is being called 2 times. Static means you have to specify the Class name before you can call it. Unless you are apart of that class.

Users.getFirstName() (example)

I d say the Cross-Reference thingy is bad. Here s the method you are looking for:

DataAware.updateUI(Collection dataAwares)

When you call the static version of this method you are not using it within the context of an instantiated object. This is the reason you need to pass a parameter to the static method so that it knows which objects to update the UI for.

That parameter will allow the static updateUI method to iterate through a collection of DataAware objects and call their updateUI method from a non-static context. That means that the no-parameter updateUI doesn t need a reference to the object because it belongs to an object.

updateUI() is an instance method, so you have to call it from an instance, while updateUI(Collection dataAwares) is a static method, and is called directly from the class.

And from the same source:

         /**
199      * given a collection containing DataAware objects,
200      * calls updateUI() on each memebr of the collection.
201      * @param dataAwares a collection containing DataAware

objects. 202 */

You re looking at the wrong line.

Look at line 203 ,that s the method being called.

This is the function which is called:

public static void updateUI(Collection dataAwares)

not

public void updateUI()





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签