0% found this document useful (0 votes)
3 views

JAVA Constructor

The document explains the two-step execution process of Java, involving compilation to bytecode and execution in the JVM. It details the structure and types of constructors in Java, including default and parameterized constructors, as well as constructor overloading and the differences between constructors and methods. Additionally, it discusses static members in classes and their significance in object-oriented programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

JAVA Constructor

The document explains the two-step execution process of Java, involving compilation to bytecode and execution in the JVM. It details the structure and types of constructors in Java, including default and parameterized constructors, as well as constructor overloading and the differences between constructors and methods. Additionally, it discusses static members in classes and their significance in object-oriented programming.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23

• Java, being a platform-independent programming language, doesn’t

work on the one-step compilation. Instead, it involves a two-step


execution, first through an OS-independent compiler; and second, in a
virtual machine (JVM) which is custom-built for every operating
system.
• The two principal stages are explained below:
Execution
• The class files generated by the compiler are independent of the
machine or the OS, which allows them to be run on any system. To
run, the main class file (the class that contains the method main) is
passed to the JVM and then goes through three main stages before
the final machine code is executed. These stages are:
These states do include:
• ClassLoader
• Bytecode Verifier
• Just-In-Time Compiler
• Step 5: Flow: Performs dataflow analysis on the trees from the
previous step. This includes checks for assignments and reachability.
• Step 6: Desugar: Rewrites the AST and translates away some syntactic
sugar.
• Step 7: Generate: Generates ‘.Class’ files
Compilation
• First, the source ‘.java’ file is passed through the compiler, which then encodes
the source code into a machine-independent encoding, known as Bytecode. The
content of each class contained in the source file is stored in a separate ‘.class’
file. While converting the source code into the bytecode, the compiler follows the
following steps:
• Step 1: Parse: Reads a set of *.java source files and maps the resulting token
sequence into AST (Abstract Syntax Tree)-Nodes.
• Step 2: Enter: Enters symbols for the definitions into the symbol table.
• Step 3: Process annotations: If Requested, processes annotations found in the
specified compilation units.
• Step 4: Attribute: Attributes the Syntax trees. This step includes name resolution,
type checking and constant folding.
Constructor in JAVA
Constructor
• In Java, a constructor is a block of codes similar to the method. It is
called when an instance of the class is created. At the time of calling
constructor, memory for the object is allocated in the memory.

• It is a special type of method which is used to initialize the object.


Constructor
• 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..
Types of Constructor
• There are two types of constructors in Java: no-arg constructor, and
parameterized constructor.

• Note: It is called constructor because it constructs the values at the


time of object creation. It is not necessary to write a constructor for a
class. It is because java compiler creates a default constructor if your
class doesn't have any
Rules for creating Java constructor
There are two 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.
Types of Java constructors

• Java Default Constructor


• A constructor is called "Default Constructor" when it doesn't have any
parameter.
• Syntax of default constructor:
• <class_name>(){}
Types of Java constructors
• In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object
creation
//Java Program to create and call a default constructor
class Bike1 {
//creating a default constructor
Bike1()
{
System.out.println("Bike is created");}
//main method
public static void main(String args[]){
//calling a default constructor
Bike1 b = new Bike1();
}
}
Types of Java Constructors

Q) What is the purpose of a default constructor?

The default constructor is used to provide the default values to the object like 0, null,
etc., depending on the type.
Types of Java Constructors
class Student3{
int id;
String name;
//method to display the value of id and name
void display()
{ System.out.println(id+" "+name);
}
public static void main(String args[]){
//creating objects
Student3 s1=new Student3();
Student3 s2=new Student3();
//displaying values of the object
s1.display();
s2.display();
} Output: 0 null
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 type
Example of Constructor Overloading
//Java program to overload constructors

class Student5{
int id;
String name;
int age;
//creating two arg constructor
Student5(int i,String n){
id = i;
name = n;
}
//creating three arg constructor
Student5(int i,String n,int a){
id = i;
name = n;
age=a;
void display(){System.out.println(id+" "+name+" "+age);}

public static void main(String args[]){


Student5 s1 = new Student5(111,“Ali");
Student5 s2 = new Student5(222,“Hasan",25);
s1.display();
s2.display();
}
} Output: 111 Ali 0
222 Hasan 25
Difference between constructor and method in Java

Java Constructor Java Method


A constructor is used to initialize the state A method is used to expose the behavior
of an object. of an object.
A constructor must not have a return type. A method must have a return type.
The constructor is invoked implicitly. The method is invoked explicitly.
The Java compiler provides a default The method is not provided by the
constructor if you don't have any compiler in any case.
constructor in a class.
The constructor name must be same as The method name may or may not be
the class name. same as the class name.
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
//Java program to initialize the values from one object to another object.
class Student6{
int id;
String name;
//constructor to initialize integer and string
Student6(int i,String n){
id = i;
name = n;
}
//constructor to initialize another object
Student6(Student6 s){
id = s.id;
name =s.name;
}
void display(){
System.out.println(id+" "+name);
}
public static void main(String args[]){
Student6 s1 = new Student6(111,“Ali");
Student6 s2 = new Student6(s1);
s1.display();
s2.display();
}
}
 Does Constructor return any value?
Yes, it is the current class instance (You cannot use return type yet it
returns a value).
 Can constructor perform other task other than initialization?
Yes, like object creation, starting a thread, calling a method, etc. You
can perform any operation in the constructor as you perform in the
method
 What is the purpose of Constructor class?
Java provides a Constructor class which can be used to get the internal
information of a constructor in the class. It is found in the
java.lang.reflect package.
Static Members

• The members of a class can be declared using the storage class


modifier static. These data members are shared by all instances of this
class and are stored in one place. Non-static data members are
created for each class object variable.
• The inability to declare static members of a class would have led to
the need to declare these data on the the global level of the program.
It would break the relationship between the data and their class, and
is not consistent with the basic paradigm of the OOP - joining data
and methods for handling them in a class. The static member allows
class data that are not specific to a particular instance to exist in the
class scope.

You might also like