Professional Documents
Culture Documents
Every time an object is created using the new() keyword, at least one constructor is called.
It calls a default constructor if there is no constructor available in the class. In such case,
Java compiler provides a default constructor by default.
The parameterized constructor is used to provide different values to distinct objects. However, you can
provide the same values also.
A constructor is a special method that is called when
an object of a class is created.
Since constructor can only return the object to class, it’s implicitly
done by java runtime and we are not supposed to add a return type
to it.
Static fields are declared using the static keyword and are associated with
the class itself rather than with any specific instance of the class. They are
commonly used to store data that is shared among all objects of a class,
such as configuration settings, counters, or constant values.
Since static fields are not tied to any specific instance, they can be accessed
using the class name itself, without the need for object instantiation.
When it comes to initializing static fields, Java offers a few different approaches. Let's discuss
each of them in detail:
Initialization at the Point of Declaration:
The simplest way to initialize a static field is to provide an initial value directly at the point of
declaration. For example:
Static Initialization Blocks:
Sometimes, initializing a static field requires more complex logic or
multiple statements. In such cases, we can use static initialization
blocks, denoted by the static keyword followed by a block of code
enclosed in curly braces.
The code inside the block is executed when the class is loaded, after the
initializations at the point of declaration.
Initialization Using Static Methods:
Another approach to initializing static fields is by using static methods. These
methods can be called explicitly or within a static block to perform initialization
tasks. By encapsulating the initialization logic in a static method, you can improve
code readability and separate concerns.
It's important to note that the order of static field initialization
follows the order of their appearance in the code. If one static
field relies on another static field, make sure the dependent field
is initialized before the dependent one.
• Flexibility: You can easily change the type of the • A composition-based design has a lesser number
contained object at runtime. of classes.
How It Works:
• 1. Define a class (the whole) with an instance variable of the
type of the other class (the part).
• 2. Create the contained object and assign it to the instance
variable in the constructor or other methods of the whole
class.
• 3. The whole class can interact with the contained object
through its methods.
EXAMPLE:
LET'S CONSIDER A CAR CLASS THAT has-a Engine class:
Nested Classes
in Java
In Java, nested classes
are a way to define
classes inside other
classes. They provide a
mechanism to group
related functionality
together and improve
code organization.
More about
Nested Classes
• Java nested class is a class which is declared inside the class or interface.
• We use inner classes to logically group classes and interfaces in one place so that it can
be more readable and maintainable.
• Additionally, it can access all the members of outer class including private data members
and methods. (non-static)
TYPES OF NESTED
CLASSES
There are the types of nested classes, the non-static,
local, anonymous, and static nested classes.