0

Let’s say that I run a program I made that uses four different classes (denoted with public class ExampleClass1{*code inside*}) in their own .java files.

Do all four of these classes get stored in the Method Area of the Heap on start-up?

Or does only the class that with the main method that I ran get stored into the Heap on start-up and the other three classes wait to get stored, until they have code that needs to be run inside of them (i.e. object creation using their constructors, and using the object's methods)?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
ZenPyro
  • 89
  • 7
  • @MartinZeitler It does almost entirely! I see now that the classes you make are not all loaded until they are needed to be used in the program. I am still unsure where the classes I make are stored inside the heap. Was I correct that they are stored inside the `Method Area` of the heap, or are they stored elsewhere? Also is the `file.class` that the JVM makes on program startup, have all the classes? – ZenPyro Mar 26 '22 at 00:23
  • 1
    Out of curiosity, why do you care? Beyond that, please understand that you are supposed to do some research prior posting, and the official JVM spec is very clear what is stored in the method area: https://docs.oracle.com/javase/specs/jvms/se8/html/jvms-2.html#jvms-2.5.4 – GhostCat Mar 26 '22 at 10:01
  • 1
    **PART 1** @GhostCat Two things! I also did a lot of supplemental reading on the JRE, JVM, JDK, and the Java Architecture in general. Which helped me understand bytecode, and source codes journey from getting compiled into bytecode -> then the JIT Compiler turning the bytecode into machinecode. Which also led me to understanding where the classpath is located, and how it is all used. As for why I care, to be perfectly honest, I am just extremely interested in actually understanding the innerworkings of what im doing, and not just saying "If I cant see it, it doesnt matter to me", Ive been on – ZenPyro Mar 26 '22 at 16:27
  • 1
    **PART 2** @GhostCat a three day journey down the rabbit whole of the Java Architecture, It all started with me wanting to know if Objects(not their reference/pointer variables) are stored on the stack or heap(which peaked my interest because the next subject in my Data Structures class is on the topic of the heap and Trees and Binary Search Tree and such, so I thought why not get a little early "self-learning" in), and after learning that I kept going down the Architecture wanting to know how it all works together in unison, to compile and run! – ZenPyro Mar 26 '22 at 16:32
  • @GhostCat I read your account, and your history sounds super interesting, and I love that you said "my main love is still Java" because I was initially turned off to Java by so many people saying they hated it, but not really ever explaining why. And now that I have been using it, and understanding its inner-workings, I'm starting to love it! Is there a reason everyone hates on Java? – ZenPyro Mar 26 '22 at 16:37
  • 2
    Great comments, first of all. Too many people just don't care about "inner workings". Apply some stuff they find on the interwebs, and then wonder when things break apart later on and they don't have any clue. Yeah, that inner drive to *understand* what is going on, that is so important. So Java ... I guess it is an age thing. Before I met Java in university, we were using obscure functional languages with unstable tooling. And then C++. Which gave you a segfault if you put the wrong char here or there. – GhostCat Mar 27 '22 at 04:21
  • 1
    With Java in the early days; it was just fresh, and surprisingly stable. Simple, but powerful. Of course, over the years, a lot of things got added, and not every decision the language owners made really worked out. And to a certain degree, it became the new COBOL. And you know, young people today want to ride a Tesla sports car. They don't understand why "them boomers" still love their clumsy semi truck that maybe needs a lot of fossil fuel, but allows them to do everything and go everywhere. – GhostCat Mar 27 '22 at 04:24
  • 1
    So, for me personally: a good programmer can (more easily than in other languages, imho) write "good code" with Java: code that runs fast (enough), is on the right level of abstraction to focus on the BUSINESS problem, and that is easy to read&understand&enhance. Other languages (like C++) somehow get you to always think about "how to make it as fast as possible", instead of first going "what is the problem we want to solve?". On the other hand, stuff like Scala went overboard, easily leading to code that only a computer science PhD person can really understand. – GhostCat Mar 27 '22 at 04:27
  • 1
    @GhostCat Sorry for the late reply, I was celebrating my fathers birthday! Thank you so much for the encouraging words, it not only helps me to push forward, but push forward harder! And ahhhh I see now, so people have their reasons, but in the end Java is great (to me and most definitely you, it is haha). Having used: C#, Lua, and Java, I can most definitely tell you Im having my favorite time with Java. I totally agree with your sentiments that Java without a doubt lets you write "good code" – ZenPyro Mar 29 '22 at 02:35
  • 1
    @GhostCat (Im one of those few young people that drive one of those darned "old" BMW e30s, so what can you expect :)). Thank you again for your knowledgeable response! – ZenPyro Mar 29 '22 at 02:36
  • 1
    You are very welcome! – GhostCat Mar 29 '22 at 04:13
  • 1
    I extended the duplicates list, as these other questions have answers going more into detail. Maybe, [this answer](https://stackoverflow.com/a/39268133/2711488) should be the first to read, as it is important to know the terms and distinguish the different operations. – Holger Apr 06 '22 at 11:43

0 Answers0