2

We talked many times about a class, object and an instance. But i don't understand them, exactly. And i want to know memory allocation of them, too. So i read the JVM spec.

There are only expressions like "symbolic reference to a class, a field, a interface, a method, an array" (not object, instance) I know that an array, class instance ∈ object.

  1. Anyway, i understood that the class, field interface method array ... are own properties to deal with a data. (As we can sort a material for own property, we can sort a data for own property....)
  2. And i understood that the object, instance are states of some data(class, field, interface...).
  3. Thus, they(class, field, interface, method, array) can be an object and an instance.

Are three statements right?

These questions derive from where they (class, object, instance, .class file) are allocated in memory.

  1. In memory, where are they allocated, exactly? ( heap, stack, method area, Run-Time Constant Pool ...)

Thx for reading my question.

bycho88
  • 21
  • 2

1 Answers1

1

Simplified, the only concept from your list that actually has a concrete in-memory representation is the Object and its members.

An Instance is a Relationship, more or less.

A Class is a code construct basically. Though to complicate things, you may have Objects in memory that represent class definitions. But they're still objects.

Definitions...

Class

https://en.wikipedia.org/wiki/Class_(computer_programming)

A class is a code construct mainly. At runtime objects are created using this code template.

In other words the class is the blueprint and the object is the building.

When an object is created by a constructor of the class, the resulting object is called an instance of the class, and the member variables specific to the object are called instance variables, to contrast with the class variables shared across the class.

Just consider Classes are the actual code. But when you're tracing or running your program you are dealing with Objects in memory.

Object

https://en.wikipedia.org/wiki/Object_(computer_science)

An object is a location in memory. Objects only exist in memory. If it's in code, then it's a class or something similar. If it's on disk, then it's the serialized representation of that object, but not the object itself.

Java JVMs typically allocate objects on the Heap. Java Primitives ( int, byte, char, etc ) may be on the stack. This may change in Java 10 ( Valhalla ), with Java Value Objects. But for now, that is normally the case.

JVM 8 Spec. Section 2.2

The Java Virtual Machine contains explicit support for objects. An object is either a dynamically allocated class instance or an array. A reference to an object is considered to have Java Virtual Machine type reference. Values of type reference can be thought of as pointers to objects. More than one reference to an object may exist. Objects are always operated on, passed, and tested via values of type reference.

Basically, it's the regular Object-Oriented definition of an Object.

Instance

This is the least important term I believe. But the following answer does a good job...

The difference between Classes, Objects, and Instances

Basically an Instance is a relationship. Specifically the relationship between an Object ( concrete ) and a Class ( template ).

Class File

https://en.wikipedia.org/wiki/Java_class_file

A class file is simply the File-System object that holds your actual binary byte code. For most intents and purposes, the class file is only exists on the file system. Unless you're dealing with your build system, you shouldn't need to deal with class files much.

File containing Java bytecode that can be executed on the Java Virtual Machine (JVM). A Java class file is produced by a Java compiler from Java programming language source files (.java files) containing Java classes. If a source file has more than one class, each class is compiled into a separate class file.

Community
  • 1
  • 1
kervin
  • 11,672
  • 5
  • 42
  • 59
  • Thx you for ur answer. But I dont understand your answer about Class File. – bycho88 Sep 19 '15 at 17:08
  • Class File is consist of bytecodes. And Class File is not a common ELF(Executable and Loadable File) but a executable and loadable file on JVM. Thus, JVM must load some partial data of the class file even if not all the class file. Look at the following phrase in JVM Spec. – bycho88 Sep 19 '15 at 17:09
  • { 2.5.4 Method Area The Java Virtual Machine has a method area that is shared among all Java Virtual Machine threads. The method area is analogous to the storage area for compiled code of a conventional language or analogous to the "text" segment in an operating system process. It stores per-class structures such as the run-time constant pool, field and method data, and the code for methods and constructors, including the special methods (§2.9) used in class and instance initialization and interface initialization. } – bycho88 Sep 19 '15 at 17:10
  • "Thus, JVM must load some partial data of the class file " ==> i think "JVM must load many portions of the class file (more than 50%) on RAM. "... – bycho88 Sep 19 '15 at 17:16
  • Yes, the bytecode is similar to machine code and loaded into the JVM. But my answer relates to your application and not the JVM. In theory, your bytecode is the JVMs data. But for Java Programming purposes, your bytecode is your application's code or text segment, as the spec points out. When talking about application memory we're generally not talking about the code or text segment memory. – kervin Sep 19 '15 at 20:05