🚀 Core Java Learning Journey Explored Variable Shadowing in Java ☕ 🔹 What is Variable Shadowing? Variable shadowing occurs when a local variable or parameter has the same name as an instance variable, hiding (shadowing) the instance variable within its scope. 📌 Why does it happen? - When method parameters or local variables use the same name as class-level variables - The local variable gets higher priority inside the method 📌 Example: class Student { int id; Student(int id) { id = id; // Shadowing happens here } } 👉 In this case, the parameter "id" shadows the instance variable "id", so the instance variable is not initialized correctly. 🔹 How to Resolve Shadowing? ✅ Use "this" keyword to refer to the current object’s instance variable class Student { int id; Student(int id) { this.id = id; // Correct way } } 🎯 Key Takeaway: Variable shadowing can lead to logical errors, and using "this" keyword helps in clearly distinguishing instance variables from local variables. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #OOP #VariableShadowing #Programming #LearningJourney #FullStackDevelopment
Java Variable Shadowing Explained with Example
More Relevant Posts
-
🚀 Core Java Learning Journey Explored Constructor Overloading in Java ☕ 🔹 What is Constructor Overloading? Constructor overloading means having multiple constructors in the same class with different parameter lists (different number, type, or order of parameters). 📌 Why use Constructor Overloading? ✅ Allows creating objects in different ways ✅ Provides flexibility in initialization ✅ Improves code readability and reusability 📌 Example: class Student { int id; String name; Student() { // No-argument constructor id = 0; name = "Default"; } Student(int id) { // Constructor with one parameter this.id = id; } Student(int id, String name) { // Constructor with two parameters this.id = id; this.name = name; } } 💡 Each constructor is called based on the arguments passed during object creation. 🎯 Key Takeaway: Constructor overloading helps in initializing objects in multiple ways, making programs more flexible and efficient. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #ConstructorOverloading #OOP #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored Ways to Initialize an Object in Java ☕ 🔹 Object initialization means assigning values to the instance variables of an object. 📌 Ways to Initialize an Object: ✅ 1. Using Reference Variable - Values are assigned after object creation class Student { int id; String name; } Student s = new Student(); s.id = 101; s.name = "Java"; --- ✅ 2. Using Method - Values are initialized using a method class Student { int id; String name; void setData(int i, String n) { id = i; name = n; } } --- ✅ 3. Using Constructor - Values are assigned at the time of object creation class Student { int id; String name; Student(int i, String n) { id = i; name = n; } } --- 🎯 Key Takeaway: Objects can be initialized in multiple ways, but using constructors is the most efficient and commonly used approach. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #OOP #Constructors #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored the "this" keyword in Java ☕ 🔹 What is "this" keyword? "this" is a reference variable that refers to the current object of a class. It is used to access instance variables and methods of the current object. 📌 Uses of "this" keyword: ✅ 1. To refer instance variables - Used when local variables or parameters have the same name (avoids shadowing) class Student { int id; Student(int id) { this.id = id; } } --- ✅ 2. To call current class methods void display() { this.show(); } --- ✅ 3. To invoke current class constructor Student() { this(100, "Java"); } --- ✅ 4. To pass current object as argument method(this); --- 🎯 Key Takeaway: "this" keyword helps in clearly referring to the current object and avoids confusion between instance and local variables. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #ThisKeyword #OOP #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored Methods in Java ☕ 🔹 What is a Method? A method is a block of code that performs a specific task and executes when it is called. It helps in improving code reusability and readability. 📌 Syntax of a Method: returnType methodName(parameters) { // method body } --- 📌 Types of Methods: ✅ 1. Predefined Methods - Already available in Java libraries - Example: "System.out.println()" ✅ 2. User-defined Methods - Created by the programmer --- 📌 Based on Parameters & Return Type: ✔️ Method with no parameters & no return value ✔️ Method with parameters & no return value ✔️ Method with parameters & return value ✔️ Method with no parameters & return value --- 📌 Example: class Demo { void display() { // method System.out.println("Hello Java"); } } --- 🎯 Key Takeaway: Methods help in breaking down complex problems into smaller tasks, making code more organized and reusable. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Methods #Programming #OOP #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
Solved the Java End-of-File (EOF) Challenge! Today I worked on a Java problem that reads input until End-of-File (EOF), and it turned out to be a great learning experience 😅 I faced multiple challenges: 1.Understanding how hasNext() works 2.fixing errors like Scanner showing red (missing imports) 3.Output formatting mistakes (order and spacing matter a lot!) 4.Debugging small syntax errors Key Concept I Learned: hasNext() does not read input — it only checks if more data is available. It is actually part of the Iterator concept in Java, where: hasNext() → checks if next element exists next() → retrieves the next element In Scanner, hasNext() works similarly by checking if more input is available before reading it. What I learned: Reading input until EOF using Scanner Understanding hasNext() and Iterator behavior Importance of correct output formatting Debugging step by step This problem taught me that even small mistakes can lead to wrong answers, but fixing them improves problem-solving skills 💻✨ #Java #HackerRank #CodingJourney #Debugging #Learning #Freshers #Programming
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored Variables in Java ☕ 🔹 What is a Variable? A variable is a container used to store data values in a program. Each variable has a name, type, and value. 📌 Types of Variables in Java: ✅ Local Variables - Declared inside methods, constructors, or blocks - Accessible only within that scope - Must be initialized before use ✅ Instance Variables - Declared inside a class but outside methods - Belong to objects - Each object has its own copy ✅ Static Variables - Declared using "static" keyword - Shared among all objects of the class - Memory allocated only once 💡 Example: "int age = 21;" "String name = "Java";" 🎯 Key Takeaway: Variables are the basic building blocks of any Java program used to store and manage data efficiently. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Variables #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored Explicit and Implicit Packages in Java ☕ 🔹 Implicit Package (Default Package) - When no package statement is written, the class belongs to the default (unnamed) package - Java automatically places the class in this package - Suitable for small programs or beginners 📌 Example: class Demo { public static void main(String[] args) { System.out.println("Default Package"); } } 👉 No "package" statement → implicit/default package --- 🔹 Explicit Package - Created by the programmer using the "package" keyword - Helps organize classes into a proper structure - Used in real-world applications 📌 Example: package com.myapp; public class Demo { public static void main(String[] args) { System.out.println("Explicit Package"); } } --- 📌 Key Differences: ✅ Implicit → No package statement, simple usage ✅ Explicit → Defined using "package", better organization ✅ Implicit → Not suitable for large projects ✅ Explicit → Preferred for scalable applications --- 🎯 Key Takeaway: Explicit packages provide better structure and scalability, while implicit packages are mainly used for simple or beginner-level programs. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Packages #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored Packages in Java ☕ 🔹 What is a Package? A package in Java is a namespace that groups related classes and interfaces together, helping in better organization and management of code. 📌 Why use Packages? ✅ Avoids class name conflicts ✅ Improves code organization ✅ Provides access control (using access modifiers) ✅ Promotes reusability 📌 Types of Packages: ✅ Built-in Packages - Provided by Java - Example: "java.lang", "java.util", "java.io" ✅ User-defined Packages - Created by the programmer 📌 How to create a package: package com.myapp; public class Demo { public void display() { System.out.println("Hello Package"); } } 📌 How to use a package: import com.myapp.Demo; class Test { public static void main(String[] args) { Demo d = new Demo(); d.display(); } } 🎯 Key Takeaway: Packages help in organizing large applications into smaller, manageable units and make code more structured and reusable. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Packages #Programming #OOP #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
🚀 Core Java Learning Journey Explored Instance, Static, and Local Variables in Java ☕ 🔹 Local Variables - Declared inside methods, constructors, or blocks - Accessible only within that specific scope - Must be initialized before use - Stored in stack memory 🔹 Instance Variables - Declared inside a class but outside methods - Belong to each object of the class - Each object has its own copy - Stored in heap memory 🔹 Static Variables - Declared using the "static" keyword - Shared among all objects of the class - Only one copy exists - Stored in method area 💡 Example: class Demo { int instanceVar = 10; // Instance variable static int staticVar = 20; // Static variable void display() { int localVar = 5; // Local variable System.out.println(localVar); } } 🎯 Key Takeaway: Understanding variable types helps in writing efficient and optimized Java programs by managing memory and scope effectively. Learning and growing at Dhee Coding Lab 💻 #Java #CoreJava #Variables #OOP #Programming #LearningJourney #FullStackDevelopment
To view or add a comment, sign in
-
Continuing my #100DaysOfCode journey while learning Object-Oriented Programming concepts in Java. On Day 28 & Day 29, I focused on understanding Inheritance and Object Relationships in Java, which are important concepts in Object-Oriented Programming. 📌 Topics Covered 🔹 Inheritance in Java – Acquiring properties and methods from a parent class 🔹 Types of Inheritance – Single, Multilevel, Hierarchical 🔹 Has-A Relationship – Understanding object relationships (Aggregation) 🔹 Introduction to Wrapper Classes 📌 Practice Implemented programs using inheritance Created examples for single and multilevel inheritance Practiced examples demonstrating Has-A relationship Explored basic usage of Wrapper Classes 💡 Key Takeaway Concepts like inheritance and object relationships help in writing reusable and structured code, which is a key advantage of Object-Oriented Programming. Step by step gaining a deeper understanding of OOP concepts in Java. #100DaysOfCode #100DaysCodingChallenge #AIPoweredJavaFullStack #JavaFullStack #JavaDeveloper #LearningInPublic #SoftwareDevelopment #FlmEdutech #FrontlinesMedia Frontlines EduTech (FLM) Fayaz S
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