One cool thing I just understood about the static members in Java.... Instance fields live inside objects, which means each object has its own copy. But Static fields, on the otherhand live in the class, and all objects(instance(s) of the class) share one copy. From the example below, So if I create a "firstEmployee" and "secondEmployee" objects, I won't be able to access the "counter" field from those objects. But I would be able to do it directly on the Employee class. I can access the counter and check how many employees have been created, which in this case, is two. So always remember that Static members belong to the class, not the objects of the class. They ideally don't have access to individual object data. Hope this distinction makes sense? #mikeTriesJava #java #springboot #backend
Java Static Fields vs Instance Fields: Understanding the Difference
More Relevant Posts
-
When should you use Optional in Java? 🧠 Optional is powerful but only when used intentionally. ✅ Use it when a method may or may not return a value. If something can be absent, make that absence explicit. Returning null hides the risk. Returning Optional makes the contract clear. It tells the caller: “Handle this properly.” 🔍 That’s good API design. But don’t overuse it 🚫 • Not as entity fields • Not in DTOs • Not as method parameters • Not as a blanket replacement for every null Optional is a design tool not decoration. Simple rule: Use Optional for return types where absence is valid. Avoid spreading it across your entire data model. Clean code isn’t about using more features. It’s about using the right ones with clarity. ⚙️✨ #Java #BackendDevelopment #CleanCode #SoftwareEngineering #SpringBoot
To view or add a comment, sign in
-
☕ #ThinkingInJava — Post No. 5 💡 Tricky Java Question What will be the output? class Test { public static int m1() { int i = 10; try { return i; } finally { i = 20; System.out.println("finally block executed"); } } public static void main(String[] args) { System.out.println(m1()); } } ✅ Output finally block executed 10 🤔 Why not 20? When return i executes, Java first saves the return value internally. temp = i // temp = 10 Then the "finally" block runs, changing i to 20. But the method returns the saved value (10). 🎯 Key Concept 👉 The return value is evaluated before the `finally'. #Java #TestAutomationSpecialist #AutomationMeetsFuture
To view or add a comment, sign in
-
🔥 DAY 17 – Cleaner Java with Streams Java Streams make collection handling elegant. Example: List<String> names = users.stream() .map(User::getName) .collect(Collectors.toList()); Why use Streams? ✔ Less boilerplate ✔ Functional style ✔ Cleaner logic But don’t overuse it for complex logic. Readable > Fancy. #Java #CleanCode
To view or add a comment, sign in
-
🔥 Day 6 — Deadlocks in Java: How They Happen & How to Avoid Them Your system is running fine… Suddenly everything stops responding. No errors. No logs. Just stuck. 👉 You might be dealing with a deadlock. ⚠ What is a Deadlock? A situation where two or more threads are waiting on each other forever. Each thread holds a lock and waits for another lock → 👉 Result: System freeze 💻 Simple Example Thread 1: synchronized(lock1) { synchronized(lock2) { // do work } } Thread 2: synchronized(lock2) { synchronized(lock1) { // do work } } 👉 Thread 1 waits for lock2 👉 Thread 2 waits for lock1 ❌ Both wait forever → DEADLOCK ⚠ Common Causes - Inconsistent lock ordering - Nested synchronization - Holding locks for too long - Multiple resources with dependencies ✅ How to Prevent Deadlocks ✔ Always follow consistent lock order ✔ Avoid nested locks when possible ✔ Use tryLock() with timeout ✔ Keep critical sections small ✔ Prefer higher-level concurrency utilities 💡 Architect Insight: Deadlocks are dangerous because: ❌ No exception thrown ❌ Hard to reproduce ❌ Often appear only under load In production, they can cause: - Complete system halt - API timeouts - Revenue impact (especially in payments) 🚀 Rule of Thumb: Design your locking strategy upfront — 👉 Don’t “fix” concurrency later 👉 Have you ever debugged a deadlock? How did you identify it? #100DaysOfJavaArchitecture #Java #Concurrency #SoftwareArchitecture #Microservices
To view or add a comment, sign in
-
-
📌 Part 2: Processes vs Threads (And Why Java Developers Must Understand This) This is where backend engineers often get confused. 🔹 What is a Process? A Process = Independent program in execution. It has: Own memory space Own heap Own stack Own resources Example: If you run: Chrome IntelliJ MySQL Each is a separate process. In Java: When you start JVM → OS creates a process. 🔹 What is a Thread? Thread = Lightweight unit of execution inside a process. Threads share memory Threads share heap But each has its own stack In Java: Thread t = new Thread(); You are asking OS (through JVM) to create a native thread. 🔥 Why This Matters in Backend Development? Spring Boot app: One process (JVM) Multiple threads (request handling) Tomcat: Uses thread pool Each HTTP request → handled by one thread So when you configure: server.tomcat.threads.max=200 You are indirectly controlling OS-level threads. ⚠️ Context Switching When CPU switches from: Thread A → Thread B OS: Saves registers Saves program counter Loads new context This costs time. Too many threads → Too much context switching → Performance drop. This is why: ExecutorService Thread pools Virtual threads (Project Loom) exist. #Java #JVM #JavaDeveloper #BackendDevelopment #SpringBoot #Concurrency #Multithreading #PerformanceEngineering #MemoryManagement #Threading
To view or add a comment, sign in
-
The next major Timefold Solver version (2.x) will bring official support for the Java Platform Module System (JPMS). We already had Automatic-Module-Name, but we went full on module-info.java style. Learn more about that here: https://lnkd.in/e_bAkw4P
To view or add a comment, sign in
-
You can now optionally use Timefold Solver with java's modulepath, instead of the classpath (which is also still supported). This enables smaller builds.
The next major Timefold Solver version (2.x) will bring official support for the Java Platform Module System (JPMS). We already had Automatic-Module-Name, but we went full on module-info.java style. Learn more about that here: https://lnkd.in/e_bAkw4P
To view or add a comment, sign in
-
💡 Tired of NullPointerException? Meet Optional in Java. For years, Java developers have battled: ❌ Unexpected null values ❌ Endless if checks ❌ Fragile code Then came Java 8 with a smarter solution → Optional. It’s not just a wrapper - it forces us to handle the absence of a value intentionally. 🚀 Why Optional matters ✔ Cleaner, more expressive code ✔ Fewer null checks ✔ Reduced NullPointerException risk ✔ Clearer API design ✔ A step toward functional & modern Java 🔴 Before Optional if (user != null && user.getAddress() != null) { System.out.println(user.getAddress().getCity()); } 🟢 With Optional Optional.ofNullable(user) .map(User::getAddress) .map(Address::getCity) .ifPresent(System.out::println); ⚠️ Use Optional for return types - not for fields, parameters, or serialization. ✨ Small change. Massive improvement in readability, safety, and intent. Optional isn’t just a class - it’s a mindset shift toward writing robust, intentional Java code. #Java #Java8 #Optional #CleanCode #FunctionalProgramming #SoftwareDevelopment
To view or add a comment, sign in
-
Very interesting to see that java applies Levenshtein edit distance algorithm for JVM XX flags options but not the java launcher options. Is something stopping us to do so ? jdk-25.jdk/Contents/Home/bin/java -XX:+TiereddCompilation Unrecognized VM option 'TiereddCompilation' Did you mean '(+/-)TieredCompilation'? Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. YOU CAN CLEARLY SEE THE HELP. BUT HERE jdk-25.jdk/Contents/Home/bin/java -list-module Unrecognized option: -list-module Error: Could not create the Java Virtual Machine. Error: A fatal exception has occurred. Program will exit. YOU CAN"T SEE THE HELP. In the end, both are not able to create JVM, so even the launcher option can "help" us. #java25 #jdk25 #jvm #java
To view or add a comment, sign in
More from this author
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
Senior dev