Java Notes
Java Notes
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.
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.
A constructor in Java is a special method that is used to initialize objects. The constructor is called
when an object of a class is created. It can be used to set initial values for object attributes:
Example
Create a constructor:
public Main() {
Main myObj = new Main(); // Create an object of class Main (This will call the constructor)
2. Parameterized constructor
Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
<class_name>(){}
In this example, we are creating the no-arg constructor in the Bike class. It will be invoked at the time of object creatio
class Bike1{
Bike1(){System.out.println("Bike is created");}
//main method
class Student3{
int id;
String name;
s1.display();
s2.display();
The parameterized constructor is used to provide different values to distinct objects. However,
you can provide the same values also.
In this example, we have created the constructor of Student class that have two parameters. We
can have any number of parameters in the constructor.
class Student4{
int id;
String name;
id = i;
name = n;
s1.display();
s2.display();
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.
class Student5{
int id;
String name;
int age;
id = i;
name = n;
id = i;
name = n;
age=a;
s1.display();
s2.display();
1. Private: The access level of a private modifier is only within the class. It cannot be accessed
from outside the class.
2. 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.
3. Protected: The access level of a protected modifier is within the package and outside the
package through child class. If you do not make the child class, it cannot be accessed from
outside the package.
4. 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.
There are many non-access modifiers, such as static, abstract, synchronized, native, volatile,
transient, etc. Here, we are going to learn the access modifiers only.
1) Private
The private access modifier is accessible only within the class.
In this example, we have created two classes A and Simple. A class contains private data member and
private method. We are accessing these private members from outside the class, so there is a
compile-time error.
class A{
A obj=new A();
If you make any class constructor private, you cannot create the instance of that class from outside
the class. For example:
class A{
2) Default
If you don't use any modifier, it is treated as default by default. The default modifier is accessible
only within package. It cannot be accessed from outside the package. It provides more accessibility
than private. But, it is more restrictive than protected, and public.
In this example, we have created two packages pack and mypack. We are accessing the A class from
outside its package, since A class is not public, so it cannot be accessed from outside the package.
//save by A.java
package pack;
class A{
void msg(){System.out.println("Hello");}
}
//save by B.java
package mypack;
import pack.*;
class B{
In the above example, the scope of class A and its method msg() is default so it cannot be accessed
from outside the package.
3) Protected
The protected access modifier is accessible within package and outside the package but through
inheritance only.
The protected access modifier can be applied on the data member, method and constructor. It can't
be applied on the class.
In this example, we have created the two packages pack and mypack. The A class of pack package is
public, so can be accessed from outside the package. But msg method of this package is declared as
protected, so it can be accessed from outside the class only through inheritance.
//save by A.java
package pack;
public class A{
//save by B.java
package mypack;
import pack.*;
class B extends A{
obj.msg();
}
Output:Hello
4) Public
The public access modifier is accessible everywhere. It has the widest scope among all other
modifiers.
//save by A.java
package pack;
public class A{
//save by B.java
package mypack;
import pack.*;
class B{
obj.msg();
Output:Hello
If you are overriding any method, overridden method (i.e. declared in subclass) must not be more
restrictive.
class A{
obj.msg();
3. Block
4. Nested class
o 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.
o The static variable gets memory only once in the class area at the time of class loading.
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.
class Student{
String name;
//constructor
rollno = r;
name = n;
//we can change the college of all objects by the single line of code
//Student.college="BBDIT";
s1.display();
s2.display();
Output:
111 Karan ITS
In this example, 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.
//which get memory each time when we create an object of the class.
class Counter{
int count=0;//will get memory each time when the instance is created
Counter(){
count++;//incrementing value
System.out.println(count);
//Creating objects
}
Program of counter by static variable
As we have mentioned above, static variable will get the memory only once, if any object changes
the value of the static variable, it will retain its value.
class Counter2{
static int count=0;//will get memory only once and retain its value
Counter2(){
System.out.println(count);
//creating objects
If you apply static keyword with any method, it is known as static method.
o A static method belongs to the class rather than the object of a class.
o A static method can be invoked without the need for creating an instance of a class.
o A static method can access static data member and can change the value of it.
class Student{
int rollno;
String name;
college = "BBDIT";
}
rollno = r;
name = n;
//creating objects
s1.display();
s2.display();
s3.display();
//Java Program to get the cube of a given number using the static method
class Calculate{
return x*x*x;
}
int result=Calculate.cube(5);
System.out.println(result);
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.
class A{
System.out.println(a);
class A2{
System.out.println("Hello main");