In my previous post, I introduced Classes and Objects — the foundation of 𝐎𝐛𝐣𝐞𝐜𝐭-𝐎𝐫𝐢𝐞𝐧𝐭𝐞𝐝 𝐏𝐫𝐨𝐠𝐫𝐚𝐦𝐦𝐢𝐧𝐠 (𝐎𝐎𝐏) in Java. But a structure alone isn’t enough. A mobile phone isn’t useful just because it exists 📱 It becomes useful when it can make calls, send messages, or play music. That behavior is what methods bring to a class. ⚙️ 𝐌𝐞𝐭𝐡𝐨𝐝𝐬 A method is a 𝒃𝒍𝒐𝒄𝒌 𝒐𝒇 𝒄𝒐𝒅𝒆 that performs a specific task. Instead of writing the same logic again and again, we define it once inside a method and simply call it whenever needed. This makes programs: ✔ cleaner ✔ reusable ✔ easier to maintain 🔹 Common Types of Methods in Java • No parameters, no return value • With parameters, no return value • No parameters, with return value • With parameters, with return value Each type helps a program interact with data in different ways. I’ve attached simple examples for each type of method in the code snippet below 👇 Next, I’ll explore Constructors — how objects receive their initial values the moment they are created. #Java #CoreJava #OOP #Methods #Programming #LearningJourney #BuildInPublic
Java Methods: Blocks of Code for Specific Tasks
More Relevant Posts
-
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
-
-
🚀 Day 9 of My Logic Building Journey in Java Today’s challenge was to build a Shopping Discount System based on different price ranges — a simple problem that actually tests real logic thinking. 🛒 Problem: Apply discounts based on total purchase amount: Above ₹5000 → 20% discount Above ₹2000 → 10% discount Otherwise → No discount 💡 What I Learned: How to break real-world problems into conditions Importance of writing logic step-by-step before coding Using if-else effectively for range-based problems ⚡ Challenges I Faced: Initially confused about the order of conditions (which condition to check first) Took time to understand edge cases like exact values (₹2000, ₹5000) Realized that wrong condition order can give incorrect results 📊 Time Complexity: O(1) — Constant time since we are only using simple conditions (no loops or recursion) 📈 Key Takeaway: Logic building is not about syntax, it's about thinking clearly and structuring the solution properly. 💻 Tech Used: Java #Java #DSA #CodingJourney #ProblemSolving #Learning #PlacementPreparation #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Just implemented the Factory Method Design Pattern in Java! In this project, I focused on understanding how to decouple object creation from business logic by applying the Factory Method pattern. Instead of directly instantiating objects, I used a factory approach to make the code more flexible, scalable, and easier to maintain. 📌 What I learned: How Factory Method improves code structure The importance of abstraction in OOP Writing cleaner and more reusable code This is part of my journey as a Computer Engineering student to strengthen my software design and backend development skills. 💡 Always open to feedback and learning! 🔗 GitHub: https://lnkd.in/dMYjh8gT #Java #DesignPatterns #SoftwareEngineering #Backend #OOP
To view or add a comment, sign in
-
Solved LeetCode 17 – Letter Combinations of a Phone Number using backtracking in Java. Approach: Mapped each digit (2–9) to its corresponding characters using a simple array for O(1) access. Then used backtracking to build combinations digit by digit. For every digit: Pick each possible character Append → explore next digit → backtrack Key idea: Treat it like a tree of choices, where each level represents a digit and branches represent possible letters. Key learnings: Backtracking = build → explore → undo StringBuilder helps avoid unnecessary string creation Problems like this are about systematic exploration of choices Time Complexity: O(4^n * n) Space Complexity: O(n) recursion stack + output Consistent DSA practice is strengthening pattern recognition day by day. #Java #DSA #Backtracking #LeetCode #CodingInterview #SoftwareEngineering
To view or add a comment, sign in
-
-
🚀 Day 97 of My 100 Days LeetCode Challenge | Java Today’s problem was a solid exercise in matrix processing and prefix sum optimization. The goal was to count the number of submatrices whose sum is less than or equal to a given value (k). A brute-force approach would be too slow, so the key was to use 2D prefix sums to efficiently compute submatrix sums. By converting the matrix into a prefix sum matrix, we can calculate the sum of any submatrix in constant time, making the overall solution much more efficient. ✅ Problem Solved: Count Submatrices With Sum ≤ K ✔️ All test cases passed (859/859) ⏱️ Runtime: 7 ms 🧠 Approach: 2D Prefix Sum 🧩 Key Learnings: ● Prefix sums are powerful for optimizing repeated range sum queries. ● 2D prefix sums extend the same idea from arrays to matrices. ● Preprocessing can drastically reduce computation time. ● Avoiding brute force is key in large input problems. ● Matrix problems often become easier with the right transformation. This problem reinforced how preprocessing techniques like prefix sums can turn complex problems into efficient solutions. 🔥 Day 97 complete — sharpening matrix optimization and prefix sum skills. #LeetCode #100DaysOfCode #Java #PrefixSum #Matrix #Algorithms #ProblemSolving #DSA #CodingJourney #Consistency
To view or add a comment, sign in
-
-
🚀 Understanding Java Multithreading Through a Real-Life Scenario This morning, I found myself doing three tasks together: ☕ Making tea 🍽 Washing utensils 🪥 Brushing teeth And suddenly it clicked… 👉 This is how Processes and Multithreading work in Java. 💡 Technical Breakdown: 🔹 Process Each independent activity can be treated as a process Making tea → Process Washing utensils → Process Brushing teeth → Process 👉 A process is an independent program with its own memory space 🔹 Thread Now, inside one task (like making tea), there are smaller steps: Boiling water Adding tea leaves Pouring milk 👉 These are threads (smaller units of execution inside a process that share memory) ⚡ Key Difference: Process → Independent & heavy Thread → Lightweight & shared 🧠 Important Insight: ⚠️ I wasn’t truly doing everything at the exact same time ⚠️ I was rapidly switching between tasks 👉 This is Concurrency, not true Parallelism ⚠️ Real-world Challenge: If multiple threads try to use the same resource simultaneously → Race Condition Example: Two people trying to use the same kettle at the same time 🧠 Key Takeaways: ✔ Multithreading exists in real life ✔ Process vs Thread is fundamental ✔ Concurrency ≠ Parallelism ✔ ExecutorService simplifies thread management ✔ Synchronization is required for shared resources 🚀 What I’m exploring next: Synchronization & Thread Safety Race Conditions in depth Real-world backend use cases 📌 Final Thought: Don’t just learn programming — learn to connect it with real-world systems. #Java #Multithreading #Concurrency #BackendDeveloper #SoftwareEngineering #LearningJourney
To view or add a comment, sign in
-
-
Coming from Java, I was always thinking like this “One class can extend only one class”. If I need more behavior, I need to use interfaces. Then, when I started Dart, I got confused again 😅 'extends' felt familiar, just like Java: If Dog extends Animal → Dog automatically gets eat(), sleep() from Animal So no confusion here. It’s simple inheritance. But then I saw “with … mixins” the inheritance things changed for me. Mixin in Dart = reuse behavior hastily Example: “class Dog extends Animal with Run, Swim” Now Dog can run AND swim without needing inheritance 'Run' and 'Swim' Here, Dog != Run, also Dog !=Swim still 'Dog' can access methods of Run and Swim. This is the power of 'Mixin' Also in Java, multiple inheritance is not allowed, to do that I would use interfaces and default methods for this. But in Dart I can indirectly use methods from multiple classes through mixins. Also in mixins, I can define that which class can access my mixin class by 'on' keyword. This is one of the interesting power mixin has, where normal interface implementation do not have such type of restriction power. That’s the difference, the simple way I understand now: extends = what the object is. mixin = what the object can do. #Flutter #Dart #Java #Mixin #OOP #LearningInPublic
To view or add a comment, sign in
-
-
Your switch-case multiplied again. Here's the refactoring that kills it. When your type code only affects data - not behavior - you can replace switch-case with a class. Not if-else chains, not pattern matching. A class where each type is an instance that carries its own data. This works in Java, JavaScript, Kotlin, C# - any language with classes. The idea is simple: if price, weight, and label all depend on a type code, why scatter that knowledge across three switch statements? Put it in one place. Let the constructor enforce completeness. Add a new type? Create a new instance. The compiler tells you what's missing. No grep. No "did I forget a case somewhere". (When types also change behavior, you need a different approach. That's the next post, next week.) I walked through the full refactoring with a pizza example. From int constants to enums to smart classes. Link in the first comment. #cleancode #refactoring #softwarecraft #codequality
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
-
-
When an object is created in Java, it needs some initial values to start working. Who assigns those values? 𝑆𝑎𝑑𝑙𝑦… 𝐽𝑎𝑣𝑎 𝑑𝑜𝑒𝑠𝑛’𝑡 𝑟𝑒𝑎𝑑 𝑜𝑢𝑟 𝑚𝑖𝑛𝑑𝑠 𝑦𝑒𝑡 😅 That’s where constructors come in. ⚙️ 𝐂𝐨𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐨𝐫𝐬 A constructor is a special method in Java that is automatically executed when an object is created. Its main purpose is to initialize the object with the required values. For example, when we create a mobile object 📱, it may need values like: • brand • price Without constructors, we would have to create the object first and then assign values separately. A constructor allows us to set those values at the time of object creation, making the code cleaner and easier to manage. 🔹 𝐓𝐲𝐩𝐞𝐬 𝐨𝐟 𝐂𝐨𝐧𝐬𝐭𝐫𝐮𝐜𝐭𝐨𝐫𝐬 𝐢𝐧 𝐉𝐚𝐯𝐚 • Default Constructor – assigns default values to an object • Parameterized Constructor – allows passing values while creating the object I’ve attached a simple example in the code snippet below to show how constructors work 👇 #Java #CoreJava #OOP #Constructors #Programming #LearningJourney #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