Behind the scenes: How Java objects are created:- What happens step by step inside the JVM: The Java source code is compiled and .class files are generated. When the program runs, Animal.class and AnimalMain.class are loaded into the Method Area (Metaspace). A java.lang.Class object is created in the Heap, which holds runtime metadata of the class. JVM creates the main thread and its stack. When, new Animal() is executed: Memory is allocated in the Heap Area. All instance variables are initialized with default values (age = 0, legs = 0). The constructor is executed. A reference to the newly created object is returned. This reference is stored in the stack variable buzo. Note:- Heap → stores objects Stack → stores references and method frames Method Area / Metaspace → stores class metadata Important: ->The reference variable does not store the actual object or a memory address. ->HashCode is generated only when hashCode() is called, not during object creation. Learning Java internals makes concepts much clearer. #Java #JVM #CoreJava #ObjectOrientedProgramming #ComputerScience #objects
Java Object Creation: JVM Steps
More Relevant Posts
-
Constructors in Java : • A Constructor is a special method used to initialize objects. • It has the same name as the class and no return type. • Constructors are called automatically when an object is created. Types of Constructors in Java: 1. Default Constructor • Has no parameters. • Initializes objects with default values. • Provided by the compiler if no constructor is defined. 2. Parameterized Constructor • Accepts parameters. • Used to initialize objects with specific values. • Helps in setting data at the time of object creation. 3. Private Constructor • Declared using the private keyword. • Restricts object creation outside the class. • Commonly used in Singleton design pattern. 4. Copy Constructor • Initializes an object using another object of the same class. • Used to copy values from one object to another. • Achieved by passing an object as a parameter. #Java #ConstructorsInJava #OOP #JavaBasics #FullStackJava
To view or add a comment, sign in
-
-
Understanding Execution Flow and Constructors in Java Today I explored how Java executes static blocks, instance blocks, and constructors when objects are created. Key concepts I practiced: 1--> Static Block Runs only once when the class is loaded. It always prints before anything else in the class. 2--> Instance Block Executes every time an object is created, and it runs before the constructor. Useful when multiple constructors share common initialization logic. 3--> Default & Parameterized Constructors The default constructor initializes objects without arguments The parameterized constructor allows passing custom values By printing messages in each block, I clearly understood Java’s execution order: -->Static block -->Static method -->Instance block -->Constructor -->Other methods Attaching the console output below for clarity. Thanks Prasoon Bidua Sir for the guidance and support in understanding core fundamentals. #Java #Constructors #OOP #ExecutionFlow #CoreJava #LearningInPublic
To view or add a comment, sign in
-
-
📘 Day 2: Object Orientation (OOP Concepts) Object Orientation is all about viewing the world as a collection of objects. 🔹 Everything in Java is treated as an object 🔹 Each object belongs to a class • Class → Blueprint (imaginary) • Object → Real-world entity 🔹 Objects have: • State(attributes) • Behavior(methods) Objects are created using the new keyword, while the JVM manages memory allocation behind the scenes. 📘 Day 3: The "main" Method The main method is the entry point of every Java program. The Operating System starts program execution from here. 🔹 Syntax: "public static void main(String[] args)" 🔹 Breakdown: ✔ public – Accessible to the OS ✔ static– No object needed ✔ void – No return value ✔ main– Recognized by JVM ✔ String[] args– Command-line arguments ✨ In short: Object-Oriented concepts + Main method = Strong foundation of Java programming #Java #ObjectOrientedProgramming #MainMethod #JavaFullStack #CodingJourney #TAPACADEMY #SoftwareDevelopment #Programming #TechLearning #JVM
To view or add a comment, sign in
-
📘 Day 9 – Advanced Java: Inheritance & Object-Oriented Hierarchy Inheritance in Java is more than code reuse — it’s about designing scalable, maintainable, and extensible systems. 🔍 Today’s deep dive included: ✅ Why inheritance matters in enterprise applications ✅ IS-A vs HAS-A relationships (real-world modeling) ✅ Constructor execution order in the JVM ✅ Types of inheritance in Java (single, multilevel, hierarchical) ✅ Method overriding rules & covariant return types ✅ How JVM selects methods at runtime (v-table concept) 💡 Key Insight: A strong understanding of inheritance is the foundation for mastering polymorphism, frameworks like Spring & Hibernate, and clean architecture design. 🚀 Next up: Day 10 – Polymorphism (Compile-time vs Runtime) #Java #AdvancedJava #OOP #Inheritance #JavaDeveloper #Programming #SoftwareEngineering #LearningJourney #PabitraTechnology
To view or add a comment, sign in
-
Most confusing fundamental concepts in Java: == vs equals(). Key Learnings: • == operator - Compares references for objects (memory location) - Compares values for primitives - For objects, it checks whether both references point to the same object • equals() method - Defined in Object class - Default behavior compares references - Many classes like String, Integer, wrapper classes, and collections override equals() to compare actual values/content • Why this matters - Two objects can be different in memory but still be logically equal - Example: new String("Java") == new String("Java") → false new String("Java").equals("Java") → true • Important rule - If a class overrides equals(), it should also override hashCode() - This is critical for correct behavior in HashMap and HashSet Final takeaway: Use == for primitive comparison. Use equals() for object content comparison. Always know which equals() implementation is being executed. Strong fundamentals make debugging easier and code more reliable. #Java #CoreJava #Equals #HashCode #Programming #SoftwareEngineering #LearningJourney #100DaysOfLearning
To view or add a comment, sign in
-
-
In continuation of my previous post on Virtual Threads. Creating virtual threads is very straightforward in Java. We don’t need to manage thread pools or worry about how many threads we are creating. We can create a virtual thread directly using: Thread thread = Thread.startVirtualThread(() -> { /* some task */ }); Or by using an executor service: ExecutorService executor = Executors.newVirtualThreadPerTaskExecutor(); executor.submit(() -> { /* some task */ }); If you run this example, you may never see the output of your thread. Your program may finish execution even before the thread prints anything. This happens because virtual threads are “daemon threads” and the main thread does not wait for daemon threads to finish execution. To prevent the JVM from exiting before the virtual threads complete, you can do: thread.join(); Or, Use try-with-resources when using an executor service, because closing the executor waits for all submitted tasks to finish. How do you manage virtual threads let me know in the comments!
To view or add a comment, sign in
-
JVM (Java Virtual Machine) – How Java Actually Runs Your Code Today I revised the internal workflow of the JVM and understood how .class files are loaded, verified, executed, and managed in memory. 1. Class Loader Subsystem Responsible for bringing .class files into memory. It has three main loaders: -->Bootstrap ClassLoader – loads core Java classes -->Extension ClassLoader – loads extension libraries -->Application ClassLoader – loads user-defined classes After loading, JVM performs: -->Verification – checks bytecode safety -->Preparation – allocates memory for static fields -->Resolution – replaces symbolic references -->Initialization – runs static blocks and static variables 2. Runtime Data Areas JVM internally divides memory into: -->Method Area: class code, static data, metadata -->Heap: objects + instance variables -->Stack: method calls + local variables (per thread) -->PC Registers: next instruction of each thread -->Native Method Stack: C/C++ code used via JNI 3. Execution Engine -->Handles actual execution: -->Interpreter: line-by-line execution -->JIT Compiler: converts hotspots into machine code for speed -->Garbage Collector: frees unused memory 4. Native Method Interface (JNI) Allows Java to interact with native libraries (C/C++). Understanding the JVM is essential to writing efficient, memory-safe Java code. Thanks Prasoon Bidua sir for making JVM fundamentals easy to grasp. #Java #JVM #CoreJava #MemoryManagement #LearningInPublic
To view or add a comment, sign in
-
-
Everyone talks about how to use the this keyword in Java… but very few explain why we actually need it. Let’s understand this with a simple thought process 👇 🔴 The Problem (without this) Imagine a class where the constructor parameters have the same name as instance variables. Java gets confused: 👉 Am I assigning the value to the variable inside the constructor or to the object itself? Result? The object’s data does not get updated the way you expect. 🟡 What’s Really Going Wrong? Java always gives priority to local variables over instance variables. So without clarity, the object never knows “which variable is mine?” 🟢 The Solution (this keyword) The this keyword clearly tells Java: 👉 “I’m talking about the current object’s variable.” Now the assignment is clear, clean, and correct. 💡 In simple words: this removes confusion between object data and temporary data. Once you understand why this exists, you’ll never forget how to use it. #Java #JavaBasics #OOP #LearningJava #ProgrammingConcepts #CodingJourney
To view or add a comment, sign in
-
-
📁 File Handling 🌊ByteStreams = flow of data as bytes 👉 Byte streams handle raw bytes 🧱 👉 FileInputStream → read 📥 👉 FileOutputStream → write 📤 👉 read() returns -1 at EOF 🛑EndOfFile 👉 Temp variable avoids skipping bytes 🔁 👉 String ➝ getBytes() → byte[] 🔄 👉 Append mode using true ➕ • read() returns 0–255 or -1, so return type is int • int -1 allows JVM to safely signal EndOfFile 🧠 java.io.File 📦 is the package that imports File Class ⚠️ Why try-catch exists • Files live outside JVM 💽 • JVM can’t guarantee file exists, path is valid, or permission is granted 🚫 • So Java forces you to handle risk using checked exceptions 🛡️ • Byte streams → raw data 🧱 • Character streams → text + encoding 📝 👉 Byte streams move raw bytes between JVM and external systems of any format Byte streams deal with bytes, character streams deal with characters using encoding. GitHub Link: https://lnkd.in/eur5pBx4 🔖Frontlines EduTech (FLM) #Java #FileHandling #Streams #BackendDevelopment #JVM #LearningInPublic #ResourceManagement #AustraliaJobs #SwitzerlandJobs #NewZealandJobs #USJobs #BackendDevelopment #Programming
To view or add a comment, sign in
-
-
Advanced Java – Day 1 Day 1 was less about “advanced” stuff and more about getting into the core •Revisited the basics that actually matter: •How Java works internally (source code ➡️ bytecode ➡️JVM) •Why Java is platform independent •Difference between JDK, JRE, and JVM •Primitive vs non-primitive data types •Core OOP concepts like class, object, encapsulation, abstraction, and polymorphism •How memory works (stack, heap, static, string pool) I also practised these concepts by building a simple calculator program to understand how logic, methods, and objects actually come together and a constrain based if-else problem. Seeing it run made things clearer. Looking forward to learning more step by step. #Java #LearningInPublic #AdvancedJava #ProgrammingBasics #StudentLife #Consistency
To view or add a comment, sign in
-
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development