开发者

Understanding this way of instantiating a new object

开发者 https://www.devze.com 2023-03-14 17:35 出处:网络
I just started BlackBerry development and I follow some of the tutorials to become familiar with the UI objects and so on and I saw/tried this:

I just started BlackBerry development and I follow some of the tutorials to become familiar with the UI objects and so on and I saw/tried this:

private MenuItem menuItemClose = new MenuItem(new StringProvider("Contacts"), 0, 0) {开发者_运维问答
    public void run() {
        onClose();
    }
};

I have not seen this way of instantiating an object before (thinking about new MenuItem), could someone explain what is happening?

And what is the difference between instantiating objects inside method definitions and in "instance variable section" like this?


That's called an "anonymous inner class."

Anonymous inner classes are classes which are created within another class but not named. They are instantiated by their interface or abstract base class definition and given their missing implementation inline with the instantiation.

In this case, MenuItem is abstract - it's missing its run() method. You're providing the implementation of its run() method here on lines 2-4.

Take a look at this JavaWorld article for more information about the various types and uses of inner classes.

As for the second part of your question "what is the difference between instantiating objects inside method definitions and in "instance variable section" like this?" -- the difference is scope, and when the object is instantiated.

Non-static member variables with initial values are created when the object which contains them is instantiated. The initial value assignment (initialization) executes at that time as well.

Static member variables with initial values are created and initialized when the class is loaded by the VM's class loader. With eager ClassLoading, this will occur at the start of the application. With lazy ClassLoading, this will occur the first time the class is referenced by the application. I believe by default most classes that aren't part of the Java runtime are loaded lazily.

Both static and non-static member variables have object-level scope and are accessible by other objects according to their access modifier (public/private/protected).

Non-static member variables are part of an object instance, and as such they are marked for garbage collection when that instance is orphaned or goes out of scope. Static member variables are only garbage collected if the class that contains them is unloaded. This only occurs if the ClassLoader instance which loaded said class is garbage collected. See this question for more info about that.

Local variables (variables which are defined within a method) with initial values are created and initialized when that line is executed as part of normal method execution. They are marked for garbage collection (destroyed) once they go out of scope (after the method in which they're contained finishes executing).


This creates an anonymous inner class which extends MenuItem and overrides the run method. It is standard Java and has nothing to do with Blackberry.


When you define a new inner class inside the method call, it's known as an 'anonymous inner class'. Anonymous inner classes are useful when you don't really need a reference to the object after the initial method call.

final Object obj = new Object(); // Standard instantiation
System.out.println(obj); // Prints java.lang.Object@5c005c

// Anonymous inner class
System.out.println(new Object() { }); // Prints Foo$1@2500250

// Anonymous inner classes work with interfaces too
new Thread(new Runnable() {

    @Override
    public void run() {
        // Runnable is an interface
    }

}).start();

This is quite a common pattern and can be useful to define 'one-time-use' objects, perhaps at the expense of readability.

0

精彩评论

暂无评论...
验证码 换一张
取 消