While continuing with object oriented concepts in Java, the static keyword was an important idea to understand. Unlike instance variables that belong to individual objects, static members belong to the class itself. Things that became clear : • a static variable is shared among all objects of a class • only one copy of a static variable exists for the entire class • static members can be accessed using the class name instead of an object • static variables are useful when a value should be common for every object • they are often used for things like counters, configuration values, or shared settings A small example helps illustrate the idea: class LoanApp { static float rateOfInterest = 9.5f; } public class Test { public static void main(String[] args) { System.out.println(LoanApp.rateOfInterest); } } Here the interest rate belongs to the class rather than any specific object. Understanding the difference between instance data and class-level data made the structure of programs much clearer. #java #oop #programming #learning #dsajourney
Lakhyadeep Sen’s Post
More Relevant Posts
-
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
-
Today I Learned – Variables & Data Types in Java In Java, understanding variables and data types is fundamental for writing clean and efficient code. --> Variables Storage containers that hold data values in memory. Each variable has a name, type, and value. Example: int age = 25; String name = "Bharath"; --> Data Types Primitive Types (8 types): byte, short, int, long, float, double, char, boolean → Hold single values, memory-efficient. Reference/Object Types: Example: String, Array, Class objects → Hold addresses of objects in memory. -->Key Points Variables must be declared with a data type. Primitive types store actual values, while reference types store addresses. Using the right data type improves performance and readability. #Java #Programming #JavaDeveloper #DataTypes #Variables #SoftwareDevelopment #Coding #Developer #Tech #OOP #LearningInPublic #100DaysOfCode #BackendDevelopment #ProgrammingTips #TechCareer
To view or add a comment, sign in
-
-
Another concept that appears while studying class initialization in Java is the instance block. It behaves differently from static blocks and is tied to object creation rather than class loading. Things that became clear : • an instance block runs every time an object of the class is created • it executes before the constructor • it can be used to perform common initialization steps for objects • unlike static blocks, instance blocks run for each object created • they are part of the object initialization process A simple structure shows the execution flow : class Demo { { System.out.println("Instance block executed"); } Demo() { System.out.println("Constructor executed"); } public static void main(String[] args) { Demo d = new Demo(); } } When the object is created, the instance block executes first and then the constructor runs. Understanding this order helps in seeing how Java prepares an object step by step during creation. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
Understanding the final Keyword in Java In Java, the final keyword is used to restrict modification. Once something is declared as final, it cannot be changed in the future. 🔹 Final Variable A variable declared as final becomes a constant. Its value cannot be modified after initialization. 🔹 Final Method A method declared as final cannot be overridden by subclasses. 🔹 Final Class A class declared as final cannot be extended (inherited by another class). 💡 The final keyword helps improve security, immutability, and code reliability in Java applications. #Java #Programming #JavaDeveloper #Coding #OOP #LearningJava #ComputerScience
To view or add a comment, sign in
-
-
Another important concept while working with classes in Java is the constructor. Constructors are closely related to object creation and help initialize the data inside an object. Things that became clear : • a constructor is a special method used to initialize objects • it has the same name as the class • constructors do not have a return type • they are called automatically when an object is created • they are commonly used to set initial values for instance variables A simple example helps illustrate the idea : class Employee { String name; int age; Employee() { System.out.println("Constructor called"); } } Whenever an object of the class is created, the constructor runs automatically and prepares the object for use. Understanding constructors made it clearer how Java ensures that objects start with proper initial values. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
🧠Stack vs Heap Memory in Java - Every Developer Should Know This When learning java, understanding Stack and Heap memory makes debugging and writing efficient code much easier. 💠 Stack Memory Stack memory is used for temporary, method-specific data like local variables and method calls, and is managed automatically in a fast, Last-In, First-Out (LIFO) manner. ->Stores methods calls and local variables ->Memory is allocated and deallocated automatically ->Very fast access ->Each thread has its own stack 🧪 Example: int x = 10; 💠 Heap Memory Heap memory is used for storing all objects and instance variables, has a longer lifespan, and is managed by the Garbage Collector. -> Stores objects and instance variables ->Shared across threads ->Managed by the Garbage Collector ->Slightly slower than the stack memory 🧪 Example: User user = new User(); ⚡ Simple way to remember Stack ->Execution & temporary data Heap ->Objects & long-lived data Understanding this concept helps us to reason about memory management, performance and Garbage collection. #Java #JavaDeveloper #Programming #SoftwareEngineering #BackendDevelopment #LearningInPublic
To view or add a comment, sign in
-
-
Internal Working of HashMap in Java — How .put() Works When the .put(key, value) method is called for the first time, HashMap internally performs the following steps: Step 1 — Array of Buckets is Created An array of 16 buckets (index 0 to 15) is initialized by default. Each bucket acts as a slot to store data. Step 2 — Hash Calculation The .put() method internally calls putVal(), which receives the result of hash(key) as an argument. The hash() method calls hashCode() which calculates a hash value by using key and returns it back to hash(), which then passes it to putVal(). Step 3 — Index Generation Inside putVal(), a bitwise AND operation is performed between the hashcode and (n-1) where n = 16: index = hashCode & (n - 1) This generates the exact bucket index where the key-value pair will be stored. Step 4 — Node Creation A Node is created and linked to the calculated bucket index. Each Node contains: 🔹 hash — the hash value 🔹 key — the key 🔹 value — the value 🔹 next — pointer to the next node What happens when .put() is called again? The same hash calculation process runs. Now two scenarios are possible: Case 1 — Same Index, Same Key (Update) If the newly generated index matches an existing bucket and the existing node's key equals the new key → the value is simply updated with the new one. Case 2 — Same Index, Different Key (Collision) If the index is the same but the keys are different → a collision occurs. In this case, the new node is linked to the previous node using the next pointer, forming a Linked List inside that bucket. That's how HashMap handles collisions and maintains data internally — simple but powerful. #Java #HashMap #DataStructures #BackendDevelopment #Programming #TechLearning #JavaDeveloper
To view or add a comment, sign in
-
-
Day 5/50 | #50DaysOfCode 📍 Platform: LeetCode 💻 Language: Java ✅ 290. Word Pattern (Easy) Today’s problem focused on mapping relationships between characters and words. It helped strengthen my understanding of hash-based data structures and bijection logic. 🔎 Approach: Split the string s into individual words Check if the length of the pattern matches the number of words Use a HashMap to map each character in the pattern to a word Ensure that each character maps to only one unique word Also verify that no two characters map to the same word Return true if the mapping follows the pattern, otherwise false 📌 Example: Input: pattern = "abba", s = "dog cat cat dog" Output: true Explanation: 'a' → "dog" 'b' → "cat" This problem improved my understanding of HashMap usage, string splitting, and bijection mapping logic in Java. #DSA #LeetCode #Java #CodingJourney #ProblemSolving #Consistency #LearningJourney #50DaysOfCode #LinkedIn
To view or add a comment, sign in
-
-
🚀 Understanding Liskov Substitution Principle (LSP) with a Simple Java Example One of the most misunderstood SOLID principles is the Liskov Substitution Principle (LSP). 📌 Definition: Subclasses should be replaceable for their base class without altering the correctness of the program. 💻 Example: class Notification { public void sendnot(){ System.out.println("Welcome to the takeUforward"); } } class TextNot extends Notification { public void sendnot() { System.out.println("Welcome to the takeUforward java course"); } } class WhatsAppNot extends Notification { public void sendnot() { System.out.println("Welcome to the takeUforward DSA course"); } } public class TufNot { public static void main(String[] args) { Notification nt = new TextNot(); nt.sendnot(); } } ✅ Why this follows LSP: TextNot and WhatsAppNot can fully replace Notification No unexpected behavior is introduced The program works correctly regardless of which subclass is used ❌ When LSP is violated: If a subclass breaks expected behavior, like: class SilentNotification extends Notification { public void sendnot() { throw new UnsupportedOperationException(); } } Now substituting this class breaks the system 🚨 🧠 Key Insight: Inheritance is not just about reusing code — it's about preserving behavior. 🔥 Takeaway: Always design subclasses so that they extend behavior, not break it. #Java #OOP #SOLID #LSP #SystemDesign #Programming Raj Vikramaditya i understand topic
To view or add a comment, sign in
-
-
🚀 Java Series – Day 2 📌 Topic: Arrays.asList() vs List.of() 🔹 What is Arrays.asList()? It converts an array into a fixed-size list 👉 You can change elements but cannot add/remove 🔹 What is List.of()? It creates an immutable list 👉 No modification allowed at all 🔹 Key Differences ✔ Arrays.asList() • Fixed size list • set() allowed ✅ • add/remove ❌ • null allowed ✅ ✔ List.of() • Completely immutable • No add/remove/set ❌ • null NOT allowed ❌ 🔹 Example List<String> list1 = Arrays.asList("Java", "Python"); list1.set(1, "C++"); // ✅ Allowed List<String> list2 = List.of("Java", "Python"); list2.set(1, "C++"); // ❌ Error 🔹 Important Point 👉 Arrays.asList() → backed by array 👉 List.of() → safe & immutable 💡 Key Takeaway Use Arrays.asList() when working with arrays Use List.of() when you need fixed, safe data Consistency is the key 🔥 Day 2 complete ✅ What do you think about this? 👇 #Java #JavaDeveloper #Programming #BackendDevelopment #CodingJourney #100DaysOfCode
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