Back to articles
πŸš€ Day 11 of My Automation Journey – Constructors, Inheritance & Java Interview Questions

πŸš€ Day 11 of My Automation Journey – Constructors, Inheritance & Java Interview Questions

via Dev.tobala d kaveri

As part of my Java learning journey with my trainer, we were given a set of conceptual and tricky questions to answer. The goal was not just to memorize answers but to understand Java fundamentals deeply, especially around: Constructors Inheritance Java Features var keyword Object Creation In this blog, I’m sharing the first set of questions along with explanations and code analysis. 1️⃣ How many ways can we initialize an instance variable in Java? Instance variables can be initialized in multiple ways: Direct Initialization inside the class class Example { int x = 10; } Here the value is assigned directly during declaration. Using a Constructor class Example { int x; Example() { x = 20; } } When the object is created, the constructor assigns the value. Using a Method class Example { int x; void setValue() { x = 30; } } The value is initialized by calling a method. Using an Instance Initialization Block class Example { int x; { x = 40; } } This block runs before the constructor execute

Continue reading on Dev.to

Opens in a new tab

Read Full Article
2 views

Related Articles