BCA, Department of Information Technology and Software Development teaches JAVA Language in the fourth semester. The curriculum of the BCA Course of JIMS is very well updated. In this PDF, the Constructor topic is explained. It is one of the very important Concepts and you need to understand it thoroughly.
Handwritten Text Recognition for manuscripts and early printed texts
Unit 2 Part 1 Constructors.pdf
1. Jagannath Institute of Management Sciences
Vasant Kunj-II, New Delhi - 110070
Subject Name: BVITSD 404 : Fundamentals Of Web
Programming Using JAVA
Department of Information Technology
Created By: Dr. Arpana Chaturvedi
@Dr. Arpana Chaturvedi
2. Subject: BVITSD 404 : FUNDAMENTALS OF
WEB PROGRAMMING USING JAVA
Topic: Unit II- Constructor Handling
@Dr. Arpana Chaturvedi
4. Unit-II Constructors
▰ A constructor initializes an object when it is created. It has the same name as its
class and is syntactically similar to a method. However, constructors have no
explicit return type.
▰ Typically, you will use a constructor to give initial values to the instance variables
defined by the class, or to perform any other start-up procedures required to create
a fully formed object.
▰ All classes have constructors, whether you define one or not, because Java
automatically provides a default constructor that initializes all member variables to
zero. However, once you define your own constructor, the default constructor is no
longer used.
▰ Syntax
▰ class ClassName {
▰ ClassName() {
▰ }
▰ } @Dr. Arpana Chaturvedi
5. Unit-II Rules for creating Java constructor
There are three rules defined for the constructor.
▰ Constructor name must be the same as its class name.
▰ A Constructor must have no explicit return type.
▰ A Java constructor cannot be abstract, static, final, and synchronized.
Note: We can use access modifiers while declaring a constructor. It controls the object
creation. In other words, we can have private, protected, public or default constructor in
Java.
@Dr. Arpana Chaturvedi
6. Unit-II Types of Constructors
Java allows two types of constructors namely
▰ Default or No argument Constructors
▰ Parameterized Constructors
▰ Dynamic Constructor
▰ Copy Constructor
@Dr. Arpana Chaturvedi
7. Unit-I Default or No Argument Constructors
Default or No argument Constructors
▰ As the name specifies the no argument constructors of Java does not accept any
parameters instead, using these constructors the instance variables of a method
will be initialized with fixed values for all objects.
Rule: If there is no constructor in a class, compiler automatically creates a default
constructor.
@Dr. Arpana Chaturvedi
8. Unit-I Example: Default or No Argument
Constructors
Class with Default Constructor
@Dr. Arpana Chaturvedi
Calling Constructors to initialize objects
Output
9. Unit-I Example 2: Default Constructor
▰ Default constructor that displays the default values
@Dr. Arpana Chaturvedi
Output
10. Unit-I Example 2: Default Constructor
▰ Default constructor that displays the default values
@Dr. Arpana Chaturvedi
Output
11. Unit-II Parametrized Constructors
Parameterized Constructors:
▰ A constructor which has a specific number of parameters is called a
parameterized constructor.
Need of Parametrized Constructors:
▰ Most often, you will need a constructor that accepts one or more parameters.
Parameters are added to a constructor in the same way that they are added to a
method, just declare them inside the parentheses after the constructor's name.
▰ The parameterized constructor is used to provide different values to distinct
objects. However, you can provide the same values also.
@Dr. Arpana Chaturvedi
12. Unit-II Example of Parametrized Constructors
Class with Parametrized Constructor
@Dr. Arpana Chaturvedi
13. Unit-II Example of Parametrized Constructors
@Dr. Arpana Chaturvedi
Calling constructor to initialize objects
Output
15. Unit-I Constructor Overloading in Java
▰ In Java, a constructor is just like a method but without return type. It can also
be overloaded like Java methods.
▰ Constructor overloading in Java is a technique of having more than one
constructor with different parameter lists. They are arranged in a way that each
constructor performs a different task. They are differentiated by the compiler by
the number of parameters in the list and their types.
@Dr. Arpana Chaturvedi
18. Unit-II Java Copy Constructor
▰ There is no copy constructor in Java. However, we can copy the values from
one object to another like copy constructor in C++.
▰ There are many ways to copy the values of one object into another in Java.
They are:
▰ By constructor
▰ By assigning the values of one object into another
▰ By clone() method of Object class
@Dr. Arpana Chaturvedi
20. Unit-II Copying values without constructor
We can copy the values of one object into another by assigning the objects values to
another object. In this case, there is no need to create the constructor.
@Dr. Arpana Chaturvedi
21. Unit-II JAVA Static Keyword
▰ The static keyword in Java is used for memory management mainly. We can
apply static keyword with variables, methods, blocks and nested classes. The
static keyword belongs to the class than an instance of the class.
▰ The static can be:
▰ Variable (also known as a class variable)
▰ Method (also known as a class method)
▰ Block
▰ Nested class
@Dr. Arpana Chaturvedi
22. Unit-II JAVA Static Variable
▰ If you declare any variable as static, it is known as a static variable.
▰ The static variable can be used to refer to the common property of all objects
(which is not unique for each object), for example, the company name of
employees, college name of students, etc.
▰ The static variable gets memory only once in the class area at the time of class
loading.
▰ Advantages of static variable
▰ It makes your program memory efficient (i.e., it saves memory).
@Dr. Arpana Chaturvedi
23. Unit-II Problem without static variable
▰ class Student{
▰ int rollno;
▰ String name;
▰ String college="ITS";
▰ }
▰ Suppose there are 500 students in my college, now all instance data members will
get memory each time when the object is created. All students have its unique
rollno and name, so instance data member is good in such case. Here, "college"
refers to the common property of all objects. If we make it static, this field will get
the memory only once.
▰ Java static property is shared to all objects.
@Dr. Arpana Chaturvedi
26. Unit-II Program of the counter without static
variable
We have created an instance variable named count which is incremented in the
constructor. Since instance variable gets the memory at the time of object creation,
each object will have the copy of the instance variable. If it is incremented, it won't
reflect other objects. So each object will have the value 1 in the count variable.
@Dr. Arpana Chaturvedi
27. Unit-II Program of counter by Static Variable
@Dr. Arpana Chaturvedi
Static variable will get the memory only once, if any object changes the value
of the static variable, it will retain its value.
28. Unit-II JAVA Static Method
If you apply static keyword with any method, it is known as static method.
• A static method belongs to the class rather than the object of a class.
• A static method can be invoked without the need for creating an instance of a
class.
• A static method can access static data member and can change the value of it.
@Dr. Arpana Chaturvedi
30. Unit-II Another example of a static method
that performs a normal calculation
@Dr. Arpana Chaturvedi
31. Unit-II Restrictions for the static method
▰ There are two main restrictions for the static method. They are:
▰ The static method can not use non static data member or call non-static method
directly.
▰ this and super cannot be used in static context.
@Dr. Arpana Chaturvedi
Why is the Java main method
static?
Ans) It is because the object is
not required to call a static
method. If it were a non-static
method, JVM creates an object
first then call main() method that
will lead the problem of extra
memory allocation.
32. Unit-II JAVA Static Block
▰ Is used to initialize the static data member.
▰ It is executed before the main method at the time of classloading.
@Dr. Arpana Chaturvedi
33. Unit-I Program without main Function
▰ Can we execute a program without main() method?
▰ Ans) No, one of the ways was the static block, but it was possible till JDK 1.6.
Since JDK 1.7, it is not possible to execute a Java class without the main method.
@Dr. Arpana Chaturvedi
34. Unit-II Understanding final
The final keyword in java is used to restrict the user. The java final keyword can be
used in many context. Final can be:
▰ variable
▰ method
▰ class
The final keyword can be applied with the variables, a final variable that have no value it
is called blank final variable or uninitialized final variable. It can be initialized in the
constructor only. The blank final variable can be static also which will be initialized in
the static block only. We will have detailed learning of these.
@Dr. Arpana Chaturvedi
35. Unit-II JAVA Final Variable
If you make any variable as final, you cannot change the value of final variable(It will be
constant).
There is a final variable speed limit, we are going to change the value of this variable,
but It can't be changed because final variable once assigned a value can never be
changed.
@Dr. Arpana Chaturvedi
36. Unit-II Java final method
If you make any method as final, you cannot override it.
@Dr. Arpana Chaturvedi
37. Unit-II JAVA Final Class
If you make any class as final, you cannot extend it.
@Dr. Arpana Chaturvedi
38. Unit-II final method
▰ Is final method inherited?
▰ Yes, final method is inherited but you cannot override it. For Example:
@Dr. Arpana Chaturvedi