🚨 𝗦𝘁𝗼𝗽 𝗰𝗼𝗻𝗳𝘂𝘀𝗶𝗻𝗴 𝗳𝗶𝗻𝗮𝗹, 𝗳𝗶𝗻𝗮𝗹𝗹𝘆, 𝗮𝗻𝗱 𝗳𝗶𝗻𝗮𝗹𝗶𝘇𝗲() 𝗶𝗻 𝗝𝗮𝘃𝗮! 🚨 These three look similar but serve very different purposes. Let’s break it down 👇 🔹 𝗳𝗶𝗻𝗮𝗹 keyword • Used with variables, methods, and classes • Variable → value cannot be changed • Method → cannot be overridden • Class → cannot be inherited 🔸 𝗳𝗶𝗻𝗮𝗹𝗹𝘆 block • Part of exception handling • Always executes after try/catch • Great for cleanup code (closing files, releasing resources) 🔹 𝗳𝗶𝗻𝗮𝗹𝗶𝘇𝗲() method • Defined in Object class • Called by 𝗚𝗮𝗿𝗯𝗮𝗴𝗲 𝗖𝗼𝗹𝗹𝗲𝗰𝘁𝗼𝗿 before object is destroyed • Rarely used in modern Java (better alternatives exist) 💡 𝗘𝘅𝗮𝗺𝗽𝗹𝗲: 𝘧𝘪𝘯𝘢𝘭 𝘪𝘯𝘵 𝘹 = 10; // 𝘤𝘢𝘯𝘯𝘰𝘵 𝘤𝘩𝘢𝘯𝘨𝘦 𝘹 𝘵𝘳𝘺 { // 𝘳𝘪𝘴𝘬𝘺 𝘤𝘰𝘥𝘦 } 𝘤𝘢𝘵𝘤𝘩(𝘌𝘹𝘤𝘦𝘱𝘵𝘪𝘰𝘯 𝘦) { // 𝘩𝘢𝘯𝘥𝘭𝘦 𝘦𝘳𝘳𝘰𝘳 } 𝘧𝘪𝘯𝘢𝘭𝘭𝘺 { // 𝘤𝘭𝘦𝘢𝘯𝘶𝘱 𝘤𝘰𝘥𝘦 } @𝘖𝘷𝘦𝘳𝘳𝘪𝘥𝘦 𝘱𝘳𝘰𝘵𝘦𝘤𝘵𝘦𝘥 𝘷𝘰𝘪𝘥 𝘧𝘪𝘯𝘢𝘭𝘪𝘻𝘦() 𝘵𝘩𝘳𝘰𝘸𝘴 𝘛𝘩𝘳𝘰𝘸𝘢𝘣𝘭𝘦 { // 𝘤𝘭𝘦𝘢𝘯𝘶𝘱 𝘣𝘦𝘧𝘰𝘳𝘦 𝘎𝘊 } 📌 𝗧𝗟;𝗗𝗥: final → restriction keyword finally → cleanup block finalize() → GC hook (legacy, avoid in new code) #Java #CodingTips #Programming #SoftwareEngineering #Learning #SpringBoot #garbagecollection
Java Final, Finally, and Finalize: Understanding the Differences
More Relevant Posts
-
⏳ Day 20 – 1 Minute Java Clarity – Method Overloading vs Overriding Same name… totally different behaviour! 🤯 📌 What is Method Overloading? Same method name — different parameters — in the SAME class. 👉 Decided at Compile Time → Static Polymorphism. 📌 Example: class Calculator { int add(int a, int b) { return a + b; } double add(double a, double b) { return a + b; } int add(int a, int b, int c) { return a + b + c; } } 👉 Java picks the right method based on arguments you pass ✅ 📌 What is Method Overriding? Child class provides its OWN implementation of a parent class method. 👉 Decided at Runtime → Dynamic Polymorphism. 📌 Example: class Animal { void sound() { System.out.println("Some sound"); } } class Dog extends Animal { @Override void sound() { System.out.println("Dog barks!"); } } Animal a = new Dog(); a.sound(); // Output: Dog barks! ✅ 💡 Real-time Example: Overloading → Same coffee machine ☕ Small cup button → 100ml Large cup button → 300ml Same button name, different output based on input! Overriding → Company policy 📋 Head office says "work 9 to 5" Branch office overrides → "work 8 to 4" Same rule name, child changes the behaviour! ⚠️ Interview Trap: Can we override a static method? 👉 No! Static methods belong to the class — not the object. 👉 It's called Method Hiding, not Overriding! 💡 Quick Summary: | Feature | Overloading | Overriding | | Class | Same | Parent & Child | | Parameters | Different | Same | | Time | Compile time | Runtime | | Keyword | None | @Override | 🔹 Next Topic → Polymorphism in Java Which one confuses you more — overloading or overriding? Drop 👇 #Java #JavaProgramming #MethodOverloading #MethodOverriding #OOPs #CoreJava #JavaDeveloper #BackendDeveloper #Coding #Programming #SoftwareEngineering #LearningInPublic #100DaysOfCode #ProgrammingTips #1MinuteJavaClarity
To view or add a comment, sign in
-
-
🚀 LeetCode #1281 | Simple Logic, Strong Foundation Sometimes the easiest problems build the strongest logic 💡 🔢 Problem: Given an integer "n", return the difference between the product of its digits and the sum of its digits. 👉 Example: Input: "n = 234" Product = 2 × 3 × 4 = 24 Sum = 2 + 3 + 4 = 9 Output = 24 - 9 = 15 --- 🧠 Approach (Step-by-Step): 1. Extract digits using "% 10" 2. Add digits → Sum 3. Multiply digits → Product 4. Return "product - sum" --- 💻 Java Code: class Solution { public int subtractProductAndSum(int n) { int sum = 0; int product = 1; while(n > 0){ int digit = n % 10; sum += digit; product *= digit; n /= 10; } return product - sum; } } --- 🎯 Key Learnings: ✔ Digit extraction using "%" and "/" ✔ Difference between accumulation (sum) & multiplication (product) ✔ Clean loop logic --- 💡 Real-Life Analogy: Think of digits as daily tasks: - Sum = total effort - Product = combined impact 👉 Result shows how much impact differs from effort! --- 🔥 Why this matters? Even simple problems sharpen your fundamentals — and strong basics = strong interviews 💪 --- #LeetCode #Java #Coding #DSA #ProblemSolving #Placements #SoftwareEngineer #LearningJourney
To view or add a comment, sign in
-
🚀 How LinkedList Solves What Arrays Cannot. (https://lnkd.in/g_8fWXFq ) ➡️ An array demands contiguous memory — every element must sit next to the other. But what if memory is scattered? That's exactly where LinkedList steps in, connecting nodes across RAM using addresses. Here are the key takeaways from the LinkedList session at TAP Academy by Sharath R sir : 🔹 The Node: Every element lives in a node — an object with a data field and the address of the next (and previous) node. It's not magic, it's just object references. 🔹 Singly vs Doubly: Singly LL has one link — forward traversal only. Doubly LL has two links — bidirectional. Java's LinkedList class uses Doubly LL internally. 🔹 Initial Capacity = 0: Unlike ArrayList (initial capacity 10), LinkedList pre-allocates nothing. Every add() creates a fresh node dynamically — no contiguous block needed. 🔹 Polymorphism hiding in plain sight: new LinkedList(arrayList) works because ArrayList IS-A Collection. Parent reference + child object = loose coupling. The same concept from OOP, live inside Collections. 🔹 Iterator vs ListIterator: Iterator moves forward only. ListIterator moves both ways — but declaring it as Iterator type blocks access to hasPrevious(). That's Inheritance at work — parent references can't reach specialized child methods. Visit this Interactive webpage to understand the concept by visualization : https://lnkd.in/g_8fWXFq #Java #LinkedList #CoreJava #TapAcademy #DataStructures #OOP #Collections #LearningEveryDay #SoftwareDevelopment #Programming
To view or add a comment, sign in
-
-
Day 18 of Java : From Primitives to Proper Structure 🚀🧠 Today was a mix of small concepts… but each one added serious depth. 🔄 Autoboxing & Unboxing Java automatically converts: int → Integer (Autoboxing) Integer → int (Unboxing) No extra effort… Java handles it behind the scenes. 🎭 Abstract Classes (Deeper Understanding) Can’t create objects directly. But can define structure + some logic. They can have: • Abstract methods • Normal methods • Constructors • Static members Feels like a blueprint with some built-in logic. 📦 POJO Classes Simple. Clean. Useful. Just: • Private variables • Getters & Setters • Constructors Used everywhere to represent data. ⚠ One Public Class Rule Only one public class per file. And file name = class name. Because Java likes clarity, not confusion. Big realization today? Java is not just about writing code… it’s about structure, rules, and clean design. Day 18 and things are getting more practical every day 🚀🔥 Special thanks to Aditya Tandon Sir & Rohit Negi Sir🙌 #Java #CoreJava #OOP #Programming #LearningJourney #Developers #BuildInPublic
To view or add a comment, sign in
-
-
𝗠𝗮𝘀𝘁𝗲𝗿𝗶𝗻𝗴 𝗝𝗮𝘃𝗮’𝘀 𝘃𝗮𝗿 Recently, I explored how Java introduced Local Variable Type Inference (var) in Java 10 and how it transformed the way developers write cleaner and more expressive code. Java has traditionally been known for its verbosity. With the introduction of var through Project Amber, the language took a major step toward modern programming practices—balancing conciseness with strong static typing. 𝗪𝗵𝗮𝘁 𝗜 𝗹𝗲𝗮𝗿𝗻𝗲𝗱: • var allows the compiler to infer types from initializers, reducing boilerplate without losing type safety. • It is not a keyword, but a reserved type name, ensuring backward compatibility. • Works only for local variables, not for fields, method parameters, or return types. • The inferred type is always static and compile-time resolved—no runtime overhead. • Powerful in handling non-denotable types, including anonymous classes and intersection types. Must be used carefully: • Avoid when the type is unclear from the initializer • Prefer when the initializer clearly reveals the type (e.g., constructors or factory methods) • Enhances readability only when the initializer clearly conveys the type. 𝗞𝗲𝘆 𝘁𝗮𝗸𝗲𝗮𝘄𝗮𝘆: var is not just about writing less code—it’s about writing clearer, more maintainable code when used correctly. The real skill lies in knowing when to use it and when not to. A special thanks to Syed Zabi Ulla sir at PW Institute of Innovation for their clear explanations and continuous guidance throughout this topic. #Java #Programming #SoftwareDevelopment #CleanCode #Java10 #Developers #LearningJourney
To view or add a comment, sign in
-
Most people try to solve 3Sum using 3 nested loops. That solution works... but it's O(n³) and too slow. Today I learned how to reduce it to O(n²). 🚀 Day 81/365 — DSA Challenge Solved: 3Sum 🧠 The Idea Steps: 1. Sort the array 2. Fix one number 3. Use two pointers to find the other two numbers 4. Skip duplicates to avoid repeating triplets ⏱ Complexity Approach & Time 3 loops -> O(n³) Sort + Two pointers -> O(n²) Big improvement. 💡 What I learned today Whenever you see: • Sorted array • Pair sum • Triplet sum • Target sum Think: Sort + Two Pointers Day 81/365 complete. Still learning. Still coding. Code: https://lnkd.in/dad5sZfu #DSA #Java #LeetCode #LearningInPublic #100DaysOfCode #365Days365DSAProblems
To view or add a comment, sign in
-
Every time I opened a new Java project in VS Code, I had to set it up from scratch. I work across multiple Java repositories. IntelliJ is my main IDE, but I use VS Code and Cursor for quick navigation and AI-first coding. Lighter. Always ready. But it doesn't pick up IntelliJ configs automatically. So every new project meant: - Mapping source folders manually - Fixing JAR paths - Setting the JDK again Same steps every time. So I tried something different. Instead of writing a script, I wrote a Markdown file. Step-by-step instructions telling the AI exactly what to do. A small snippet from the file: - Check .idea at root only (ignore subdirs) - Extract source roots from .iml files - Map them to java.project.sourcePaths Now I just type /java-vscode-setup in VS Code or Cursor Agent. The AI scans the project. Generates the correct settings.json. Confirms changes before applying them. No scripts. No extra tools. Just clear instructions. This changed how I think about automation. Instead of writing scripts to do the work, you write instructions that guide the AI to do it. Same result. Less hassle. Works across every repo. Could I have done this with a Python script? Yes. But I'm already in Claude or Cursor anyway. So I built it where I work. Still experimenting with this approach. Curious - what task have you turned into a slash command? #AI #Cursor #Claude #VSCode #DevTools
To view or add a comment, sign in
-
🚀 Day 536 of #750DaysOfCode 🚀 Today I solved Count Submatrices With Equal Frequency of X and Y (LeetCode 3212) using Java. 🔹 Problem Summary: Given a grid containing 'X', 'Y', and '.', we need to count the number of submatrices starting from the top-left corner (0,0) such that: • The number of 'X' and 'Y' is equal • The submatrix contains at least one 'X' 🔹 Approach: Instead of checking every possible submatrix (which would be too slow), I used the Prefix Sum technique. I maintained two prefix matrices to store the count of 'X' and 'Y' up to each cell. For every position (i, j), I checked: countX == countY and countX > 0 If true, that submatrix is valid. This reduces the complexity to O(n × m), which works efficiently for large grids. 🔹 Key Concepts Learned: ✅ Prefix Sum in 2D ✅ Matrix traversal optimization ✅ Handling constraints up to 1000 × 1000 ✅ Clean implementation in Java Consistent practice is making problem-solving faster and more structured every day. #750DaysOfCode #Day536 #LeetCode #Java #DataStructures #Algorithms #PrefixSum #CodingChallenge #ProblemSolving
To view or add a comment, sign in
-
-
💡 Understanding Collection-Based Dependency Injection in Spring When we talk about Dependency Injection (DI) in Spring, most of us think about injecting a single object. But did you know you can inject multiple beans at once using collections? 🤔 Let’s break it down in a simple way 👇 🔹 What is Collection-Based DI? Instead of injecting one object, Spring can inject a list, set, or map of beans into your class. 👉 This is useful when you have multiple implementations of the same interface. 🔹 What’s Happening Here? ✅ Spring scans all beans of type PaymentService ✅ It collects them into a List ✅ Injects them automatically into PaymentProcessor 🔹 Other Supported Collections You can also use: ✔ List<Interface> ✔ Set<Interface> ✔ Map<String, Interface> → Key = bean name Example: Map<String, PaymentService> paymentMap; 🔹 Why is this Useful? ✔ Helps implement strategy pattern easily ✔ Avoids manual bean selection ✔ Makes code more flexible & scalable 🔹 Pro Tip 🚀 Spring injects collections in a specific order if you use @Order or @Priority. 🔚 Summary Collection-based DI = Inject multiple beans → Handle dynamically → Write cleaner code If you're learning Spring deeply, this concept is a game changer when building scalable systems 🔥 #Java #SpringBoot #DependencyInjection #BackendDevelopment #CleanCode
To view or add a comment, sign in
-
-
🚀 Day 49: Mastering the Art of Inheritance & Object References Today was all about Reference Variables vs. Actual Objects. In Java, what you create is just as important as how you refer to it. ☕ I’ve spent the day testing the boundaries of Parent and Child relationships. Here is my "Day 49 Breakdown": 🏗️ The 4 Scenarios of Object Reference 1.Parent p = new Parent(); ▫️ The standard setup. You can access all properties and methods defined in the Parent class. ▫️ Constraint: You cannot see any unique fields or methods added in the Child class. 2.Child c = new Child(); ▫️ The "All-Access" pass. Through inheritance, the child object can access its own properties AND everything inherited from the Parent. 3.Parent p = new Child(); (Upcasting) ▫️ The Power Move: You create a Child object but store it in a Parent reference. ▫️ The Rule: You can only call methods defined in the Parent class. However, if a method is overridden in the Child, the Child's version will execute at runtime! ▫️ Constraint: You cannot directly access Child-specific properties without explicit downcasting. 4.Reference Assignment (p1 = p2); ▫️ I learned how to point one reference variable to another. It doesn't create a new object; it just creates another "remote control" for the same object in memory. 💡 Key Takeaway: Inheritance isn't just about reusing code—it's about Type Hierarchy. Knowing which "remote control" (reference) to use determines what your code can actually "see" and "do." #Java #LearningInPublic #100DaysOfCode #ObjectOrientedProgramming #SoftwareEngineering #BackendDeveloper 10000 Coders Meghana M
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