BCA Class and Object

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 43

Class and Object

Class

• Class is a logical entity because when we are creating a class then


memory will not be created for a class.

• Class is a collection of data members, constructors, methods and


blocks.
Cont…
• Class is a collection of objects of same type.
Objects
• Object is a physical entity because when we are creating object then
memory will be allocated for object.

• Object is created with the help of new keyword.


Cont…
• Object is the instance of a class.
Constructors in Java
• In Java, a constructor is a block of codes similar to the method.

• It is called when object is created.

• At the time of calling constructor, memory for the object is allocated


in the memory.
Cont…
• It will be used to initialize the object.
Rules for creating Java constructor

• Constructor name must be the same as its class name.

• A Java constructor cannot be abstract, static, final, and synchronized


Types of Java Constructors

• There are 3 Types of constructors.


• Default Constructor.

• No-argument Constructor.

• Parameterized Constructor.
1. Default Constructor.
• If there is no constructor in a class, compiler automatically creates a
default constructor.
Cont…
Example
class Student
{
int id;
public static void main(String a[])
{
Student s1=new Student();
System.out.println(s1.id);
}
}
Output: 0
2. No-argument Constructor.
• A constructor that has no parameter is known as no-argument
constructor.
Example
class Bike
{
Bike()
{
System.out.println("Bike is created");
}
public static void main(String a[])
{
Bike b=new Bike();
}
}
Output: Bike is created
3. Parameterized Constructor
• A constructor which has a specific number of parameters is called a
parameterized constructor.
Example
class Student
{
int id;
String name;
Student(int i, String n)
{
id = i;
name = n;
}
void display()
{
System.out.println(id);
System.out.println(name);
}
public static void main(String a[])
{
Student s1 = new Student(111,"Karan");
s1.display();
}
}
Cont…
Output:
111
Karan
Difference between constructor and method in Java

Constructor Method

A Constructor is called implicitly by the


A Method is called by the programmer.
system.

A Constructor is called when a object is


A Method is called through method calls.
created using the keyword new.

A Constructor’s name must be same as


A Method’s name can be anything.
the name of the class.

A Constructor doesn’t have a return type. A Method must have a return type.
Ways to initialize object:

There are 3 ways to initialize object in Java.

• By reference variable
• By method
• By constructor
1) Initializing an Object by reference variable

• Initializing an object means storing data into the object.


Example
class Student
{  
 int id;  
 public static void main(String a[])
{  
   Student s1= new Student();  
    s1.id=101;  
    System.out.println(s1.id);
}
}
Output  
101
2) Initializing an Object by method
Example
class Student
{  
 int id;  
void display(int n)
{
id = n;
System.out.println(id);
}
 public static void main(String a[])
{  
   Student s1=new Student();  
    s1.display(101);  
 }
}
Cont…
Output:
101
3) Initializing an Object by a constructor
Example
class Student
{
int id;
Student(int i)
{
id = i;
}
void display()
{
System.out.println(id);
}
public static void main(String a[])
{
Student s1 = new Student(101);
s1.display();
}
}
Cont…
Output:
101
Java static keyword

• The static keyword in Java is used for memory management mainly.

• We can apply static keyword with variables, methods and blocks etc.


Cont…
• The static can be:
• Variable (also known as a class variable)

• Method (also known as a class method)

• Block
1) 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, college name of students, etc.


Cont…
• The static variable gets memory only once in the class area at the time
of class loading.
Advantage of static variable

• It makes your program memory efficient (i.e., it saves memory).


Example: Understanding the problem without static variable
class Student
{
String collegename;
Student(String c)
{
collegename = c;
}
void display ()
{
System.out.println(collegename);
}
public static void main(String a[])
{
Student s1 = new Student("GLAU");
Student s2 = new Student("GLAU");
s1.display();
s2.display();
}
}
Cont…
Output:
GLAU
GLAU
Example of static variable
class Student
{
static String collegename =“GLAU";//static variable
void display ()
{
System.out.println(collegename);
}
public static void main(String a[])
{
Student s1 = new Student();
Student s2 = new Student();
s1.display();
s2.display();
}
}
Cont…
Output:
GLAU
GLAU
2) Java static method

• If you apply static keyword with any method, it is known as static


method.

• A static method can be called without the need for creating an instance
(object) of a class.
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.


Example of static method
class A
{
static void display()
{
System.out.println("Static Method");
}
}
class B
{
public static void main(String a[])
{
A.display();

}
}
Cont…
Output:

Static Method
3) Java static block

• It is used to initialize the static data member.

• It is executed before the main method at the time of class loading.


Example of static block
class A
{
static
{
System.out.println("static block");
}
public static void main(String a[])
{
System.out.println("Hello main");
}
}
Cont…
Output:
static block
Hello main

You might also like