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

Constructors

The document provides an overview of key concepts in Java, including the 'instanceof' operator, constructors (default and parameterized), constructor overloading, access specifiers, and the static keyword. It explains how constructors initialize objects, the differences between constructors and methods, and the use of static variables and methods. Additionally, it introduces Javadoc for generating documentation from Java code.

Uploaded by

anuja.it
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

Constructors

The document provides an overview of key concepts in Java, including the 'instanceof' operator, constructors (default and parameterized), constructor overloading, access specifiers, and the static keyword. It explains how constructors initialize objects, the differences between constructors and methods, and the use of static variables and methods. Additionally, it introduces Javadoc for generating documentation from Java code.

Uploaded by

anuja.it
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 34

Java instanceof

• The java instanceof operator is used to test


whether the object is an instance of the
specified class.
• class Simple1{
• public static void main(String args[]){
• Simple1 s=new Simple1();
• System.out.println(s instanceof Simple1);//true
• }
• }
• Test it Now

• Output:true
Constructors
Constructors in Java

• It is a special type of method which is used to


initialize the object.
• 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.
• It is called constructor because it constructs the values
at the time of object creation.
Rules for creating Java 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
• There are two types of constructors in Java:
1.no-arg/Default constructor, and
2.parameterized constructor.
Java Default Constructor

• A constructor is called "Default Constructor"


when it doesn't have any parameter.

Syntax:
<class_name>()
{
}
Example 1
• //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();
• }
• }
• Output:
• Bike is created
Example 2
class Test
{
int a,b;
Test()
{
Output:
a=5; 5
b=6; 6
}
public static void main(String args[])
{
Test obj=new Test();
System.out.println(obj.a);
System.out.println(obj.b);
}
}
Default Constructor
class Student3{
int id;
String name;
Student3() 101 Rishwanth101
{ Rishwanth.
name="Rishwanth";
id=101;
}
//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();
}
}
Java Parameterized Constructor

• A constructor which has a specific number of


parameters is called a parameterized
constructor.
• The parameterized constructor is used to
provide different values to distinct objects
Example of parameterized constructor

class Student4{
int id;
String name;
//creating a parameterized constructor
Student4(int i,String n){
id = i;
name = n;
}
//method to display the values
void display(){System.out.println(id+" "+name);}
public static void main(String args[]){
//creating objects and passing values
Student4 s1 = new Student4(111,"Karan");
Student4 s2 = new Student4(222,"Aryan");
//calling method to display the values of object
s1.display();
s2.display();
}
}

Output:
111 Karan
222 Aryan
Constructor Overloading in Java

• Constructor overloading in Java


• is a technique of having more than one
constructor with different parameter lists.
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,"Karan");
Student5 s2 = new Student5(222,"Aryan",25);
s1.display();
s2.display();
}
}
Output:
111 Karan 0
222 Aryan 25
Difference between constructor and method in Java
Access Specifier
• The access modifiers/specifier in Java specifies the
accessibility or scope of a field, method, constructor, or
class.
• There are four types of Java access modifiers:
• Private: The access level of a private modifier is only
within the class. It cannot be accessed from outside the
class.
• Default: The access level of a default modifier is only
within the package. It cannot be accessed from outside
the package. If you do not specify any access level, it will
be the default.
• Protected: The access level of a protected
modifier is within the package and outside the
package through child class.
• Public: The access level of a public modifier is
everywhere. It can be accessed from within
the class, outside the class, within the package
and outside the package.
Static Keyword
• The static can be:
• Variable (also known as a class variable)
• Method (also known as a class method)
• Block
• Nested class
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
Understanding the 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.
Example of static variable

Output:
111 Karan ITS
222 Aryan ITS
Program of the counter without static
variable

Output:
1
1
1
Program of counter by static variable
static variable will get the memory only once

Output:
1
2
3
Java 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.
Output:111 Karan BBDIT
222 Aryan BBDIT
333 Sonoo BBDIT
Java - Documentation Comments

• The Java language supports three types of


comments −
What is Javadoc?

• Javadoc is a tool which comes with JDK and it


is used for generating Java code
documentation in HTML format from Java
source code, which requires documentation in
a predefined format

You might also like