Today I revisited an important Core Java concept :- Variable Shadowing. Shadowing happens when a constructor parameter has the same name as a class member variable. Example of the problem: private String name; public Employee(String name) { name = name; // Shadow problem } Here, both sides refer to the constructor parameter. The instance variable never gets assigned. Result: The object prints default values like null or 0.0. Correct way using this: public Employee(String name) { this.name = name; // Proper assignment } this refers to the current object’s instance variable. Key takeaway: Without this, constructor parameters can hide instance variables. With this, we clearly differentiate between object state and local scope. A small keyword, but critical for proper object initialization. Strong OOP fundamentals prevent subtle bugs in real systems. #Java #CoreJava #OOP #Encapsulation #SoftwareEngineering #BackendDevelopment
Java Constructor Parameter Shadowing: Understanding the Issue
More Relevant Posts
-
I was staring at all these classes - Future, FutureTask, Callable, RunnableFuture and felt completely lost. Too many classes, too many interfaces, no idea why they exist. Then I asked myself a simple question. "What is actually wrong with 𝐑𝐮𝐧𝐧𝐚𝐛𝐥𝐞?" Turns out, two things: • 𝘙𝘶𝘯𝘯𝘢𝘣𝘭𝘦 𝘤𝘢𝘯𝘯𝘰𝘵 𝘳𝘦𝘵𝘶𝘳𝘯 𝘢 𝘷𝘢𝘭𝘶𝘦. • 𝘙𝘶𝘯𝘯𝘢𝘣𝘭𝘦 𝘤𝘢𝘯𝘯𝘰𝘵 𝘵𝘩𝘳𝘰𝘸 𝘤𝘩𝘦𝘤𝘬𝘦𝘥 𝘦𝘹𝘤𝘦𝘱𝘵𝘪𝘰𝘯𝘴, 𝘵𝘩𝘦 𝘤𝘰𝘮𝘱𝘪𝘭𝘦𝘳 𝘸𝘰𝘯'𝘵 𝘦𝘷𝘦𝘯 𝘭𝘦𝘵 𝘺𝘰𝘶. So you end up writing ugly hacks like shared arrays to get results out, and wrapping everything in try-catch inside the thread with no way to tell the caller what went wrong. But here's the deeper problem nobody talks about. 𝐖𝐡𝐞𝐧 𝐚 𝐭𝐡𝐫𝐞𝐚𝐝 𝐭𝐡𝐫𝐨𝐰𝐬 𝐚𝐧 𝐞𝐱𝐜𝐞𝐩𝐭𝐢𝐨𝐧, 𝐢𝐭 𝐝𝐢𝐞𝐬 𝐬𝐢𝐥𝐞𝐧𝐭𝐥𝐲. The main thread has no idea. 𝘛𝘩𝘦 𝘦𝘹𝘤𝘦𝘱𝘵𝘪𝘰𝘯 𝘤𝘢𝘯𝘯𝘰𝘵 𝘤𝘳𝘰𝘴𝘴 𝘵𝘩𝘦 𝘵𝘩𝘳𝘦𝘢𝘥 𝘣𝘰𝘶𝘯𝘥𝘢𝘳𝘺 𝘰𝘯 𝘪𝘵𝘴 𝘰𝘸𝘯. 𝐂𝐚𝐥𝐥𝐚𝐛𝐥𝐞 𝐚𝐧𝐝 𝐅𝐮𝐭𝐮𝐫𝐞 solve exactly this. 𝐂𝐚𝐥𝐥𝐚𝐛𝐥𝐞 is just like 𝐑𝐮𝐧𝐧𝐚𝐛𝐥𝐞 but its 𝘤𝘢𝘭𝘭() method returns a value and allows checked exceptions. That's it. Nothing magical. Future is a placeholder, an object that says "𝘐 𝘥𝘰𝘯'𝘵 𝘩𝘢𝘷𝘦 𝘵𝘩𝘦 𝘳𝘦𝘴𝘶𝘭𝘵 𝘺𝘦𝘵, 𝘣𝘶𝘵 𝘐 𝘸𝘪𝘭𝘭. 𝘊𝘰𝘮𝘦 𝘣𝘢𝘤𝘬 𝘢𝘯𝘥 𝘢𝘴𝘬 𝘮𝘦 𝘭𝘢𝘵𝘦𝘳." And 𝐅𝐮𝐭𝐮𝐫𝐞.𝐠𝐞𝐭() is the bridge. Internally 𝐅𝐮𝐭𝐮𝐫𝐞𝐓𝐚𝐬𝐤 catches the exception on the child thread, stores it in a field, and rethrows it on whatever thread calls get(). That's how the exception crosses the thread boundary. Catch it on one thread. Store it. Rethrow it on another. Once I understood this, all those classes made complete sense. I recently made a video where instead of just teaching Callable and Future directly, I make viewers build these classes from scratch, so by the time they see Java's actual classes, everything feels familiar. If you are learning 𝐉𝐚𝐯𝐚 𝐌𝐮𝐥𝐭𝐢𝐭𝐡𝐫𝐞𝐚𝐝𝐢𝐧𝐠, and want to learn about Callable and Future, head to below video, it might help: 🔗 [https://lnkd.in/dkNvFA8N] What concept in Java Multithreading took you the longest to truly understand? Would love to know 👇 I have also created a complete Java Multithreading Series which you can have a look into: https://lnkd.in/gGXF-fqQ #Java #JavaProgramming #Callable #Future #Multithreading #ExecutorService
#19 Callable and Future in Java - Why Runnable is NOT Enough? #multithreadinginjava #java
https://www.youtube.com/
To view or add a comment, sign in
-
Solved a tricky Java interview-style question today involving: static variables instance variables constructors object references This example really helped me understand how Java memory works and how static data is shared across objects while instance data is unique per object. Learning by solving real problems instead of just watching tutorials. #Java #OOP #LearningByDoing #SoftwareDeveloper #InterviewPrep
To view or add a comment, sign in
-
-
🚀 Java Deep Dive — Daemon Threads (Something many developers overlook) In Java, not all threads behave the same way. There are two types: • User Threads • Daemon Threads The JVM keeps running as long as at least one user thread is alive. But daemon threads work differently. They are background service threads used for supporting tasks like: • Garbage Collection • Monitoring • Background cleanup If all user threads finish, the JVM will terminate immediately, even if daemon threads are still running. Example: Java Example Thread thread = new Thread(() -> { while(true){ System.out.println("Running..."); } }); thread.setDaemon(true); thread.start(); If the main thread finishes, this daemon thread will not keep the JVM alive. Important rule: You must call "setDaemon(true)" before starting the thread, otherwise Java throws "IllegalThreadStateException". 💡 Key Insight Daemon threads are useful for background tasks that should not block application shutdown. #Java #Multithreading #BackendDevelopment #SoftwareEngineering #LearningInPublic
To view or add a comment, sign in
-
Day 44 — LeetCode Progress (Java) Problem: Find Minimum in Rotated Sorted Array Required: Given a sorted array that has been rotated, find the minimum element in the array in O(log n) time. Idea: Use binary search by comparing the middle element with the right boundary to determine which part of the array contains the minimum. Approach: Initialize two pointers: left = 0 right = n − 1 Perform binary search while left < right. Compute the middle index: mid = left + (right - left) / 2 Compare nums[mid] with nums[right]: If nums[mid] < nums[right] → The minimum lies in the left half (including mid) → Update right = mid Otherwise → The minimum lies in the right half (excluding mid) → Update left = mid + 1 Continue until left == right. The element at that index is the minimum element in the rotated array. Time Complexity: O(log n) Space Complexity: O(1) #LeetCode #DSA #Java #BinarySearch #Algorithms #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀Java practice - Day 87 Completed! 👍 Problem: Sum of Squares of Special Elements Language: Java Today’s problem was about identifying special elements in a 1-indexed array. An element is considered special if its index divides the length of the array (n % i == 0). The task was to calculate the sum of the squares of such elements.✨ #Day87 #Java #LeetCode #Arrays #ProblemSolving #DailyCoding #Consistency #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Day 5 of My DSA Journey Today I solved Merge Two Sorted Lists from Merge Two Sorted Lists using Java. At first, I struggled with the mental model of linked list pointers. I realized that instead of creating new nodes unnecessarily, we can directly reuse the existing nodes and connect them efficiently. The key idea is to maintain a dummy node and a tail pointer: The dummy node helps simplify edge cases. The tail pointer always points to the last node of the merged list. As we compare elements from both lists, we attach the smaller node to tail.next and move the pointer forward. Here’s the solution I implemented: class Solution { public ListNode mergeTwoLists(ListNode list1, ListNode list2) { ListNode dummy = new ListNode(); ListNode tail = dummy; while(list1!=null && list2!=null){ if (list1.val<list2.val){ tail.next = list1; tail = tail.next; list1=list1.next; }else{ tail.next = list2; tail = tail.next; list2 = list2.next; } } while(list1!=null){ tail.next=list1; tail = tail.next; list1 = list1.next; } while(list2!=null){ tail.next=list2; tail = tail.next; list2 = list2.next; } return dummy.next; } } 📌 Time Complexity: O(n + m) 📌 Space Complexity: O(1) #Day5 #DSA #LeetCode #Java #LinkedList #CodingJourney
To view or add a comment, sign in
-
-
While continuing with static concepts in Java, static blocks were another interesting feature to understand. They are used when some initialization needs to happen at the class level before objects are created. Things that became clear : • a static block runs when the class is loaded into memory • it executes before the main method • it is commonly used for class level initialization • static blocks run only once, regardless of how many objects are created • they are often used when static variables need some setup logic A small example helped understand the execution order : class Demo { static { System.out.println("Static block executed"); } public static void main(String[] args) { System.out.println("Main method executed"); } } In this case, the static block runs first when the class loads, and then the main method executes. Seeing this execution flow made the class loading process in Java a little clearer. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
Interfaces also allow a class to follow multiple contracts at the same time. Unlike classes, where a class can extend only one parent class, a class in Java can implement multiple interfaces. Things that became clear : • a class can implement more than one interface • each interface can define a different set of behaviours • the implementing class must provide implementations for all the methods • this approach allows combining different capabilities in a single class • it helps achieve flexibility while avoiding multiple inheritance of classes A simple example shows how multiple interfaces can be implemented : interface ICalculator { void add(int a, int b); void sub(int a, int b); } interface IAdvancedCalculator { void mul(int a, int b); void div(int a, int b); } class CalculatorImpl implements ICalculator, IAdvancedCalculator { public void add(int a, int b) { System.out.println(a + b); } public void sub(int a, int b) { System.out.println(a - b); } public void mul(int a, int b) { System.out.println(a * b); } public void div(int a, int b) { System.out.println(a / b); } } This structure allows the class to support operations defined by multiple interfaces while keeping responsibilities organized. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
Day 10 of Java & Now I Know Where My Array Actually Lives 🧠💻 Today was not about writing arrays… It was about understanding what happens inside memory. And honestly this was powerful. 👉 Arrays are non-primitive (reference) types. That means: When we write int[] arr = new int[5]; • The variable arr lives in Stack memory • The actual array data lives in Heap memory • arr stores the reference (address) of that heap location So basically… We’re pointing to memory. 🔥 Contiguous Storage Array elements are stored next to each other in one continuous block. 5 integers = 5 × 4 bytes = 20 bytes All in one straight line. That’s why arrays are fast. ⚡ Random Access Java doesn’t search for elements. It calculates their address: Base Address + (Index × Size of Data Type) That’s why accessing the 1st element takes the same time as the 1,000,000th. O(1) access. Instant. Big realization today? Arrays aren’t just collections. They’re structured memory blocks optimized for speed. Day 10 and now I’m not just using arrays… I understand how they work internally. Leveling up every day 🚀🔥 Special thanks to Rohit Negi sir and Aditya Tandon sir🙌🏻🙌🏻 #Java #CoreJava #Arrays #Programming #LearningJourney #Developers #BuildInPublic
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