SMUsamaShah
SMUsamaShah

Reputation: 7890

What is a native peer?

Native peer is defined in Effective Java (2nd) as following

A second legitimate use of finalizers concerns objects with native peers. A native peer is a native object to which a normal object delegates via native methods. Because a native peer is not a normal object, the garbage collector doesn’t know about it and can’t reclaim it when its Java peer is reclaimed.

In another question java peer is explained with example. Is it the java class PrintHello or another class which uses PrintHello?

What part is being called a Native Peer here and which part is Java Peer, any example? I understand the use of finalize, I am only confused about which part native peer is being referred to.

Upvotes: 5

Views: 2256

Answers (2)

Andres
Andres

Reputation: 10717

A native object is not programmed only in java, but in a platform specific language, typically c or assembler.

Memory allocated by this code cannot be disposed by the GC. Therefore you may need to clean it in a finalizer.

The native peer is the native part of a Java object.

You can see a nice example here: https://www.javaworld.com/article/2077520/learn-java/java-tip-23--write-native-methods.html

Upvotes: 4

Kayaman
Kayaman

Reputation: 73558

A simple example would be a native window vs. a JFrame. A JFrame is a Java peer, but it needs a (platform dependent) native peer to actually display graphics.

This is why you need to call dispose() when getting rid of a JFrame. You need to get rid of the native component explicitly, because the GC can't touch it.

Upvotes: 3

Related Questions