SlideShare a Scribd company logo
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
system.
A Method is called by the programmer.
A Constructor is called when a object is
created using the keyword new.
A Method is called through method calls.
A Constructor’s name must be same as
the name of the class.
A Method’s name can be anything.
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
this keyword in java
• this is a reference variable that refers to the current class
object.
Usage of java this keyword
• There are many usage of java this keyword.
• this can be used to refer current class instance variable.
• this() can be used to call current class constructor.
1) this: to refer current class instance variable
• The this keyword can be used to refer current class instance
variable.
• If there is ambiguity between the instance variables and
parameters (local variable), this keyword resolves the
problem of ambiguity.
Example: Problem without this keyword
class Student
{
int x;
int y;
Student(int x, int y)
{
x = x;
y = y;
}
void display()
{
System.out.println(x);
System.out.println(y);
}
public static void main(String a[])
{
Student s1=new Student(10,20);
s1.display();
}
}
Cont…
Output: 0
0
Ex: Solution of Previous Problem with this keyword
class Student
{
int x;
int y;
Student(int x, int y)
{
this.x = x;
this.y = y;
}
void display()
{
System.out.println(x);
System.out.println(y);
}
public static void main(String a[])
{
Student s1=new Student(10,20);
s1.display();
}
}
Cont…
Output: 10
20
2) this() : to call current class constructor
• The this() can be used to call the current class constructor.
• Call to this() must be the first statement in constructor.
Example 1
class A
{
A()
{
System.out.println("hello a");
}
A(int x)
{
this();
System.out.println(x);
}
public static void main(String a1[])
{
A a=new A(10);
}
}
Cont…
Output:
hello a
10

More Related Content

Similar to BCA Class and Object (3).pptx

Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
Mochi263119
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
Sonu WIZIQ
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
Elizabeth alexander
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
Vinod Kumar
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
Lovely Professional University
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
madhavi patil
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
ssuser8347a1
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
sonukumarjha12
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
ambikavenkatesh2
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
Atul Sehdev
 
Java
Java Java
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
YakaviBalakrishnan
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
Helen SagayaRaj
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
Dr.Neeraj Kumar Pandey
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
mcollison
 
Definning class.pptx unit 3
Definning class.pptx unit 3Definning class.pptx unit 3
Definning class.pptx unit 3
thenmozhip8
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
rajkamaltibacademy
 

Similar to BCA Class and Object (3).pptx (20)

Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Core Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class pptCore Java unit no. 1 object and class ppt
Core Java unit no. 1 object and class ppt
 
Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Static keyword ppt
Static keyword pptStatic keyword ppt
Static keyword ppt
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
Java static keyword
Java static keywordJava static keyword
Java static keyword
 
packages and interfaces
packages and interfacespackages and interfaces
packages and interfaces
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
Java Programming - 04 object oriented in java
Java Programming - 04 object oriented in javaJava Programming - 04 object oriented in java
Java Programming - 04 object oriented in java
 
inheritance.pptx
inheritance.pptxinheritance.pptx
inheritance.pptx
 
object oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoMobject oriented programming using java, second sem BCA,UoM
object oriented programming using java, second sem BCA,UoM
 
ITFT-Classes and object in java
ITFT-Classes and object in javaITFT-Classes and object in java
ITFT-Classes and object in java
 
Java
Java Java
Java
 
Module 1.pptx
Module 1.pptxModule 1.pptx
Module 1.pptx
 
Java As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & AppletsJava As an OOP Language,Exception Handling & Applets
Java As an OOP Language,Exception Handling & Applets
 
C# classes objects
C#  classes objectsC#  classes objects
C# classes objects
 
Pi j2.3 objects
Pi j2.3 objectsPi j2.3 objects
Pi j2.3 objects
 
Definning class.pptx unit 3
Definning class.pptx unit 3Definning class.pptx unit 3
Definning class.pptx unit 3
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 

Recently uploaded

International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
gerogepatton
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
IJECEIAES
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
Ratnakar Mikkili
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
mahammadsalmanmech
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
SyedAbiiAzazi1
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
Madhumitha Jayaram
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
KrishnaveniKrishnara1
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
MDSABBIROJJAMANPAYEL
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
wisnuprabawa3
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
VICTOR MAESTRE RAMIREZ
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
NidhalKahouli2
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
Mukeshwaran Balu
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 

Recently uploaded (20)

International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...International Conference on NLP, Artificial Intelligence, Machine Learning an...
International Conference on NLP, Artificial Intelligence, Machine Learning an...
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
Embedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoringEmbedded machine learning-based road conditions and driving behavior monitoring
Embedded machine learning-based road conditions and driving behavior monitoring
 
Exception Handling notes in java exception
Exception Handling notes in java exceptionException Handling notes in java exception
Exception Handling notes in java exception
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
Question paper of renewable energy sources
Question paper of renewable energy sourcesQuestion paper of renewable energy sources
Question paper of renewable energy sources
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application14 Template Contractual Notice - EOT Application
14 Template Contractual Notice - EOT Application
 
Wearable antenna for antenna applications
Wearable antenna for antenna applicationsWearable antenna for antenna applications
Wearable antenna for antenna applications
 
22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt22CYT12-Unit-V-E Waste and its Management.ppt
22CYT12-Unit-V-E Waste and its Management.ppt
 
Properties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptxProperties Railway Sleepers and Test.pptx
Properties Railway Sleepers and Test.pptx
 
New techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdfNew techniques for characterising damage in rock slopes.pdf
New techniques for characterising damage in rock slopes.pdf
 
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student MemberIEEE Aerospace and Electronic Systems Society as a Graduate Student Member
IEEE Aerospace and Electronic Systems Society as a Graduate Student Member
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
basic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdfbasic-wireline-operations-course-mahmoud-f-radwan.pdf
basic-wireline-operations-course-mahmoud-f-radwan.pdf
 
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
ACRP 4-09 Risk Assessment Method to Support Modification of Airfield Separat...
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 

BCA Class and Object (3).pptx

  • 2. 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.
  • 3. Cont… • Class is a collection of objects of same type.
  • 4. 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.
  • 5. Cont… • Object is the instance of a class.
  • 6. 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.
  • 7. Cont… • It will be used to initialize the object.
  • 8. 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
  • 9. Types of Java Constructors • There are 3 Types of constructors. • Default Constructor. • No-argument Constructor. • Parameterized Constructor.
  • 10. 1. Default Constructor. • If there is no constructor in a class, compiler automatically creates a default constructor.
  • 12. Example class Student { int id; public static void main(String a[]) { Student s1=new Student(); System.out.println(s1.id); } } Output: 0
  • 13. 2. No-argument Constructor. • A constructor that has no parameter is known as no-argument constructor.
  • 14. Example class Bike { Bike() { System.out.println("Bike is created"); } public static void main(String a[]) { Bike b=new Bike(); } } Output: Bike is created
  • 15. 3. Parameterized Constructor • A constructor which has a specific number of parameters is called a parameterized constructor.
  • 16. 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(); } }
  • 18. Difference between constructor and method in Java Constructor Method A Constructor is called implicitly by the system. A Method is called by the programmer. A Constructor is called when a object is created using the keyword new. A Method is called through method calls. A Constructor’s name must be same as the name of the class. A Method’s name can be anything. A Constructor doesn’t have a return type. A Method must have a return type.
  • 19. Ways to initialize object: There are 3 ways to initialize object in Java. • By reference variable • By method • By constructor
  • 20. 1) Initializing an Object by reference variable • Initializing an object means storing data into the object.
  • 21. 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
  • 22. 2) Initializing an Object by method
  • 23. 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); } }
  • 25. 3) Initializing an Object by a constructor
  • 26. 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(); } }
  • 28. 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.
  • 29. Cont… • The static can be: • Variable (also known as a class variable) • Method (also known as a class method) • Block
  • 30. 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.
  • 31. Cont… • The static variable gets memory only once in the class area at the time of class loading.
  • 32. Advantage of static variable • It makes your program memory efficient (i.e., it saves memory).
  • 33. 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(); } }
  • 35. 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(); } }
  • 37. 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.
  • 38. 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.
  • 39. Example of static method class A { static void display() { System.out.println("Static Method"); } } class B { public static void main(String a[]) { A.display(); } }
  • 41. 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.
  • 42. Example of static block class A { static { System.out.println("static block"); } public static void main(String a[]) { System.out.println("Hello main"); } }
  • 44. this keyword in java • this is a reference variable that refers to the current class object.
  • 45. Usage of java this keyword • There are many usage of java this keyword. • this can be used to refer current class instance variable. • this() can be used to call current class constructor.
  • 46. 1) this: to refer current class instance variable • The this keyword can be used to refer current class instance variable. • If there is ambiguity between the instance variables and parameters (local variable), this keyword resolves the problem of ambiguity.
  • 47. Example: Problem without this keyword class Student { int x; int y; Student(int x, int y) { x = x; y = y; } void display() { System.out.println(x); System.out.println(y); } public static void main(String a[]) { Student s1=new Student(10,20); s1.display(); } }
  • 49. Ex: Solution of Previous Problem with this keyword class Student { int x; int y; Student(int x, int y) { this.x = x; this.y = y; } void display() { System.out.println(x); System.out.println(y); } public static void main(String a[]) { Student s1=new Student(10,20); s1.display(); } }
  • 51. 2) this() : to call current class constructor • The this() can be used to call the current class constructor. • Call to this() must be the first statement in constructor.
  • 52. Example 1 class A { A() { System.out.println("hello a"); } A(int x) { this(); System.out.println(x); } public static void main(String a1[]) { A a=new A(10); } }