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
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 – Object Orientation Rules & Main Method in Java While learning Java, I explored how object relationships work and how a program starts execution. --> HAS-A Relationship Represents composition or aggregation, where one class contains another class object as a member. Example: Car HAS-A Engine --> DOES-A Relationship Represents behavior implementation, where a class performs behavior defined by another type using interfaces or abstract classes. Example: Bird DOES-A Flyable --> Main Method in Java The entry point of a Java application where the Java Virtual Machine starts program execution. Syntax: public static void main(String[] args) Breakdown: • public → Accessible everywhere • static → Can be executed without creating an object • void → Does not return a value • main → Method recognized by JVM to start execution • String[] args → Used to receive command-line arguments #JavaDeveloper #ObjectOrientedProgramming #OOP #JavaLearning #BackendDevelopment #CodingJourney #100DaysOfCode #LearningInPublic #DeveloperCommunity #FutureDeveloper #TechCareer
To view or add a comment, sign in
-
-
While learning more about constructors in Java, the idea of a default constructor also became clearer. A default constructor is automatically provided by Java when no constructor is written in a class. Things that became clear : • if a class does not define any constructor, Java automatically creates a default constructor • the default constructor has no parameters • it mainly helps create objects without requiring initialization values • instance variables get their default values if they are not explicitly initialized • once a constructor is written manually, Java no longer provides the default one automatically A simple example shows how it works : class Student { int rollNo; String name; } public class Test { public static void main(String[] args) { Student s = new Student(); System.out.println(s.rollNo); System.out.println(s.name); } } Here, even though no constructor is written, Java still allows object creation by providing a default constructor. Understanding this behaviour helps explain why objects can still be created even when constructors are not explicitly defined. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
💡 Understanding the var Keyword in Java While learning modern Java, I came across the var keyword — a small feature that makes code cleaner, but only when used correctly. Here’s how I understand it 👇 In Java, when we declare a variable using var, the compiler automatically determines its data type based on the value assigned. For example: java var name = "Akash"; Here, Java infers that name is of type String. ⚠️ One important clarification: It’s not the JVM at runtime — type inference happens at compile time, so Java remains strongly typed. ### 📌 Key Rules of var ✔️ Must be initialized at the time of declaration java var a = "Akash"; // ✅ Valid var b; // ❌ Invalid ✔️ Can only be used inside methods (local variables) ❌ Not allowed for: * Instance variables * Static variables * Method parameters * Return types ### 🧠 Why use var? It helps reduce boilerplate and makes code cleaner, especially when the type is obvious: java var list = new ArrayList<String>(); ### 🚫 When NOT to use it Avoid `var` when it reduces readability: java var result = getData(); // ❌ unclear type ✨ My takeaway: `var` doesn’t make Java dynamic — it simply makes code more concise while keeping type safety intact. I’m currently exploring Java fundamentals and system design alongside frontend development. Would love to hear how you use var in your projects 👇 Syed Zabi Ulla PW Institute of Innovation #Java #Programming #LearningInPublic #100DaysOfCode #Developers #CodingJourney
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
-
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
To view or add a comment, sign in
-
Another way abstraction is implemented in Java is through interfaces. Interfaces define a set of methods that a class must implement, but they do not provide the actual implementation. Things that became clear : • an interface represents complete abstraction • methods declared inside an interface are implicitly public and abstract • a class uses the implements keyword to follow the rules defined by an interface • the implementing class must provide the body for all the methods • interfaces help create loose coupling between components A simple example shows the idea: interface ICalculator { void add(int a, int b); void sub(int a, int b); } class CalculatorImpl implements ICalculator { public void add(int a, int b) { System.out.println(a + b); } public void sub(int a, int b) { System.out.println(a - b); } } In this structure the interface defines what operations should exist, while the implementing class decides how those operations work. This approach makes it easier to design flexible and maintainable systems. #java #oop #programming #learning #dsajourney
To view or add a comment, sign in
-
🚀Day 21 – Understanding Variable Scope & Memory Management in Java Today I focused on concepts that play an important role in how Java programs manage variables and memory during execution. Instead of just writing programs, I explored how variables behave in different scopes and how Java automatically manages unused objects. 📚 Concepts Covered ✔ Variable Scopes • Difference between Instance Variables and Local Variables • How instance variables belong to an object and exist throughout the object's lifecycle • How local variables exist only inside methods or blocks ✔ Garbage Collection & Finalize • Understanding how Java automatically removes unused objects from heap memory • Learning how Garbage Collector helps optimize memory usage • Exploring the concept of the finalize() method and object cleanup 💡 Key Learning Understanding variable scope and garbage collection helps developers write cleaner, memory-efficient, and more reliable programs. These core concepts are essential for mastering Java, Object-Oriented Programming, and real-world software development. I’m focusing on deep understanding of concepts instead of rushing through topics, because strong fundamentals build strong developers. #Java #CoreJava #JavaProgramming #OOP #Programming #SoftwareDevelopment #CodingJourney #LearningInPublic #DeveloperJourney #TechLearning #BackendDevelopment #FutureDeveloper #BuildInPublic #Consistency
To view or add a comment, sign in
-
-
Deep Dive into Core Java Concepts 🚀 Today, I explored some important Java concepts including toString(), static members, and method behavior in inheritance. 🔹 The toString() method (from Object class) is used to represent an object in a readable format. By default, it returns "ClassName@hashcode", but by overriding it, we can display meaningful information. 🔹 Understanding static in Java: ✔️ Static variables and methods are inherited ❌ Static methods cannot be overridden ✔️ Static methods can be hidden (method hiding) 🔹 What is Method Hiding? If a subclass defines a static method with the same name and parameters as the parent class, it is called method hiding, not overriding. 🔹 Key Difference: ➡️ Overriding → applies to instance methods (runtime polymorphism) ➡️ Method Hiding → applies to static methods (compile-time behavior) 🔹 Also revised execution flow: ➡️ Static blocks (Parent → Child) ➡️ Instance blocks (Parent → Child) ➡️ Constructors (Parent → Child) This learning helped me clearly understand how Java handles inheritance, memory, and method behavior internally. Continuing to strengthen my Core Java fundamentals 💻🔥 #Java #OOP #CoreJava #Programming #LearningJourney #Coding
To view or add a comment, sign in
-
-
Another small but interesting concept while studying interfaces was marker interfaces. Unlike normal interfaces, marker interfaces do not define any methods. Instead, they act as a signal to the Java runtime that a class has a certain capability. Things that became clear : • marker interfaces are empty interfaces without methods • they are used to "mark" a class so that the JVM treats it differently • they provide additional behaviour without forcing method implementation • some well-known examples in Java include Cloneable and Serializable • they are mainly used by libraries or the runtime to enable certain features A simple structure looks like this : interface IDemo { } class Test implements IDemo { } Here the interface itself does not contain any methods, but implementing it can allow a class to receive special handling in certain situations. Understanding marker interfaces showed another way Java uses interfaces beyond just defining behaviour. #java #oop #programming #learning #dsajourney
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