SlideShare uma empresa Scribd logo
1 de 20
UNIT - III
CLASSES, INTERFACES AND
PACKAGES
Class and object:
• Defining class
• Constructor
• Method overloading
• Static members
• Nesting of methods
• this keyword
• Command line argument
Defining a class
• Class is a set of objects which shares common
characteristics/ behavior and common properties/
attributes.
• Class is not a real world entity. It is just a template or
blueprint or prototype from which objects are created.
• Class does not occupy memory.
• Class is a group of variables of different data types and
group of methods.
• A class in java can contain:
• data member
• method
• constructor
• nested class and
• interface
Defining a Class in Java
• Java provides a reserved keyword class to define a class. The
keyword must be followed by the class name. Inside the
class, we declare methods and variables.
• In general, class declaration includes the following in the
order as it appears:
• Modifiers: A class can be public or has default access.
• class keyword: The class keyword is used to create a class.
• Class name: The name must begin with an initial letter
(capitalized by convention).
• Superclass (if any): The name of the class's parent
(superclass), if any, preceded by the keyword extends. A
class can only extend (subclass) one parent.
• Interfaces (if any): A comma-separated list of interfaces
implemented by the class, if any, preceded by the keyword
implements. A class can implement more than one
interface.
• Body: The class body surrounded by braces, { }.
Syntax to declare a class:
access_modifier class<class_name>
{
data member;
data method;
}
Creating objects
It is a basic unit of Object-Oriented Programming and
represents real life entities
An object consists of :
State: It is represented by attributes of an object. It also reflects
the properties of an object.
Behavior: It is represented by methods of an object. It also
reflects the response of an object with other objects.
Identity: It gives a unique name to an object and enables one
object to interact with other objects.
Classes and objects
Accessing class members:
obj_name.var_name;
obj_name.method_name();
Example:
class Student
{
int id;//data member (also instance variable)
String name; //data member (also instance variable)
public static void main(String args[])
{
Student s1=new Student();
//creating an object of Student
System.out.println(s1.id);
System.out.println(s1.name);
}
}
Creating Array of objects
• Using arrays, more than one object for the
same class can be created.
Syntax:
class_name object[]=new class_name[size];
(or)
class_name [] object=new class_name[size];
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.
• 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.
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
There are two types of constructors in Java:
• Default constructor (no-arg constructor)
• Parameterized constructor
Java Default Constructor
A constructor is called "Default Constructor" when it doesn't have any parameter.
Syntax of default constructor:
<class_name>(){}
Example:
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();
}
}
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.
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();
} }
Static members
• The members of the classes are
(a) Member variables or fields
(b) member functions or methods
• In Java, static members are those which
belongs to the class and can access these
members without instantiating the class.
• The static keyword can be used with methods,
fields, classes, blocks.
General form:
static datatype var1,var2…………………varn;
static returntype methodname(arguments)
{
------------
------------
}
Static variables belong to a class and is common for
all objects of the class. Therefore no separate memory
area for static members. Static variables behave like
global variables and can be accessed by all created
objects.
Nesting of methods
A method can be called by using only its
name by another method of the same class that
is called Nesting of Methods.
A method of a class can be called only by an
object of that class using the dot operator.
When a method in java calls another
method in the same class, it is called Nesting of
methods.
• Method1Method2…………..> methodn
class demo {
private int m, n;
demo(int x, int y)
{ m = x;
n = y; }
int largest()
{
if (m > n) return m;
else return n; }
void display()
{ int large=largest();
System.out.println("The Greatest Number is : "+large); } }
public class nested_method {
public static void main(String args[]) {
demo o =new demo(10,20);
o.display(); } }
“this” keyword in Java:
• In Java, this is a reference variable that refers to
the current object.
• “this” is an implicit pointer to every method in a
class
• “this” pointer contains the address of the object
which calls the method
• The this keyword refers to the current object in a
method or constructor.
• The most common use of the this keyword is to
eliminate the confusion between class attributes
and parameters with the same name.
class Student{
int rollno;
String name;
float fee;
Student(int rollno,String name,float fee){
this.rollno=rollno;
this.name=name;
this.fee=fee;
}
void display(){System.out.println(rollno+" "+name+" "+fee);}
}
class TestThis2{
public static void main(String args[]){
Student s1=new Student(111,"ankit",5000f);
Student s2=new Student(112,"sumit",6000f);
s1.display();
s2.display();
}}
Command line argument
• The java command-line argument is an argument
i.e. passed at the time of running the java program.
• The arguments passed from the console can be
received in the java program and it can be used as
an input.
class CommandLineExample{
public static void main(String args[])
{
System.out.println("Your first argument is: "+args[0]);
}
}

Mais conteúdo relacionado

Semelhante a UNIT - IIInew.pptx

class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptxEpsiba1
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptxEpsiba1
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxDrYogeshDeshmukh1
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slotsmha4
 
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,UoMambikavenkatesh2
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...Sagar Verma
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingNithyaN19
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorialrajkamaltibacademy
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in javaElizabeth alexander
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Mahmoud Alfarra
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxchetanpatilcp783
 

Semelhante a UNIT - IIInew.pptx (20)

Sonu wiziq
Sonu wiziqSonu wiziq
Sonu wiziq
 
Ch-2ppt.pptx
Ch-2ppt.pptxCh-2ppt.pptx
Ch-2ppt.pptx
 
class as the basis.pptx
class as the basis.pptxclass as the basis.pptx
class as the basis.pptx
 
oop 3.pptx
oop 3.pptxoop 3.pptx
oop 3.pptx
 
OOPs & Inheritance Notes
OOPs & Inheritance NotesOOPs & Inheritance Notes
OOPs & Inheritance Notes
 
constructors.pptx
constructors.pptxconstructors.pptx
constructors.pptx
 
Unit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptxUnit No 2 Objects and Classes.pptx
Unit No 2 Objects and Classes.pptx
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots03 object-classes-pbl-4-slots
03 object-classes-pbl-4-slots
 
Class and object
Class and objectClass and object
Class and object
 
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
 
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
OOPS in java | Super and this Keyword | Memory Management in java | pacakages...
 
Java chapter 4
Java chapter 4Java chapter 4
Java chapter 4
 
Lecture 5
Lecture 5Lecture 5
Lecture 5
 
Java Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overridingJava Inheritance - sub class constructors - Method overriding
Java Inheritance - sub class constructors - Method overriding
 
Corejava Training in Bangalore Tutorial
Corejava Training in Bangalore TutorialCorejava Training in Bangalore Tutorial
Corejava Training in Bangalore Tutorial
 
Object oriented programming in java
Object oriented programming in javaObject oriented programming in java
Object oriented programming in java
 
Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2Object Oriented Programming_Lecture 2
Object Oriented Programming_Lecture 2
 
Unit3 part1-class
Unit3 part1-classUnit3 part1-class
Unit3 part1-class
 
Chap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptxChap-2 Classes & Methods.pptx
Chap-2 Classes & Methods.pptx
 

Último

Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...soginsider
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdfKamal Acharya
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityMorshed Ahmed Rahath
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"mphochane1998
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsvanyagupta248
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersMairaAshraf6
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxmaisarahman1
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Servicemeghakumariji156
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueBhangaleSonal
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksMagic Marks
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxnuruddin69
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayEpec Engineered Technologies
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaOmar Fathy
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network DevicesChandrakantDivate1
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesMayuraD1
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXssuser89054b
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptMsecMca
 

Último (20)

Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
Hazard Identification (HAZID) vs. Hazard and Operability (HAZOP): A Comparati...
 
School management system project Report.pdf
School management system project Report.pdfSchool management system project Report.pdf
School management system project Report.pdf
 
A Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna MunicipalityA Study of Urban Area Plan for Pabna Municipality
A Study of Urban Area Plan for Pabna Municipality
 
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments""Lesotho Leaps Forward: A Chronicle of Transformative Developments"
"Lesotho Leaps Forward: A Chronicle of Transformative Developments"
 
AIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech studentsAIRCANVAS[1].pdf mini project for btech students
AIRCANVAS[1].pdf mini project for btech students
 
Computer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to ComputersComputer Lecture 01.pptxIntroduction to Computers
Computer Lecture 01.pptxIntroduction to Computers
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptxA CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
A CASE STUDY ON CERAMIC INDUSTRY OF BANGLADESH.pptx
 
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best ServiceTamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
Tamil Call Girls Bhayandar WhatsApp +91-9930687706, Best Service
 
Double Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torqueDouble Revolving field theory-how the rotor develops torque
Double Revolving field theory-how the rotor develops torque
 
Learn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic MarksLearn the concepts of Thermodynamics on Magic Marks
Learn the concepts of Thermodynamics on Magic Marks
 
Bridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptxBridge Jacking Design Sample Calculation.pptx
Bridge Jacking Design Sample Calculation.pptx
 
Standard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power PlayStandard vs Custom Battery Packs - Decoding the Power Play
Standard vs Custom Battery Packs - Decoding the Power Play
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Introduction to Serverless with AWS Lambda
Introduction to Serverless with AWS LambdaIntroduction to Serverless with AWS Lambda
Introduction to Serverless with AWS Lambda
 
Computer Networks Basics of Network Devices
Computer Networks  Basics of Network DevicesComputer Networks  Basics of Network Devices
Computer Networks Basics of Network Devices
 
DeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakesDeepFakes presentation : brief idea of DeepFakes
DeepFakes presentation : brief idea of DeepFakes
 
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
notes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.pptnotes on Evolution Of Analytic Scalability.ppt
notes on Evolution Of Analytic Scalability.ppt
 

UNIT - IIInew.pptx

  • 1. UNIT - III CLASSES, INTERFACES AND PACKAGES
  • 2. Class and object: • Defining class • Constructor • Method overloading • Static members • Nesting of methods • this keyword • Command line argument
  • 3. Defining a class • Class is a set of objects which shares common characteristics/ behavior and common properties/ attributes. • Class is not a real world entity. It is just a template or blueprint or prototype from which objects are created. • Class does not occupy memory. • Class is a group of variables of different data types and group of methods. • A class in java can contain: • data member • method • constructor • nested class and • interface
  • 4. Defining a Class in Java • Java provides a reserved keyword class to define a class. The keyword must be followed by the class name. Inside the class, we declare methods and variables. • In general, class declaration includes the following in the order as it appears: • Modifiers: A class can be public or has default access. • class keyword: The class keyword is used to create a class. • Class name: The name must begin with an initial letter (capitalized by convention). • Superclass (if any): The name of the class's parent (superclass), if any, preceded by the keyword extends. A class can only extend (subclass) one parent. • Interfaces (if any): A comma-separated list of interfaces implemented by the class, if any, preceded by the keyword implements. A class can implement more than one interface. • Body: The class body surrounded by braces, { }.
  • 5. Syntax to declare a class: access_modifier class<class_name> { data member; data method; }
  • 6. Creating objects It is a basic unit of Object-Oriented Programming and represents real life entities An object consists of : State: It is represented by attributes of an object. It also reflects the properties of an object. Behavior: It is represented by methods of an object. It also reflects the response of an object with other objects. Identity: It gives a unique name to an object and enables one object to interact with other objects.
  • 8. Accessing class members: obj_name.var_name; obj_name.method_name(); Example: class Student { int id;//data member (also instance variable) String name; //data member (also instance variable) public static void main(String args[]) { Student s1=new Student(); //creating an object of Student System.out.println(s1.id); System.out.println(s1.name); } }
  • 9. Creating Array of objects • Using arrays, more than one object for the same class can be created. Syntax: class_name object[]=new class_name[size]; (or) class_name [] object=new class_name[size];
  • 10. 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. • 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.
  • 11. 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 There are two types of constructors in Java: • Default constructor (no-arg constructor) • Parameterized constructor
  • 12. Java Default Constructor A constructor is called "Default Constructor" when it doesn't have any parameter. Syntax of default constructor: <class_name>(){} Example: 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(); } }
  • 13. 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. 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(); } }
  • 14. Static members • The members of the classes are (a) Member variables or fields (b) member functions or methods • In Java, static members are those which belongs to the class and can access these members without instantiating the class. • The static keyword can be used with methods, fields, classes, blocks.
  • 15. General form: static datatype var1,var2…………………varn; static returntype methodname(arguments) { ------------ ------------ } Static variables belong to a class and is common for all objects of the class. Therefore no separate memory area for static members. Static variables behave like global variables and can be accessed by all created objects.
  • 16. Nesting of methods A method can be called by using only its name by another method of the same class that is called Nesting of Methods. A method of a class can be called only by an object of that class using the dot operator. When a method in java calls another method in the same class, it is called Nesting of methods. • Method1Method2…………..> methodn
  • 17. class demo { private int m, n; demo(int x, int y) { m = x; n = y; } int largest() { if (m > n) return m; else return n; } void display() { int large=largest(); System.out.println("The Greatest Number is : "+large); } } public class nested_method { public static void main(String args[]) { demo o =new demo(10,20); o.display(); } }
  • 18. “this” keyword in Java: • In Java, this is a reference variable that refers to the current object. • “this” is an implicit pointer to every method in a class • “this” pointer contains the address of the object which calls the method • The this keyword refers to the current object in a method or constructor. • The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name.
  • 19. class Student{ int rollno; String name; float fee; Student(int rollno,String name,float fee){ this.rollno=rollno; this.name=name; this.fee=fee; } void display(){System.out.println(rollno+" "+name+" "+fee);} } class TestThis2{ public static void main(String args[]){ Student s1=new Student(111,"ankit",5000f); Student s2=new Student(112,"sumit",6000f); s1.display(); s2.display(); }}
  • 20. Command line argument • The java command-line argument is an argument i.e. passed at the time of running the java program. • The arguments passed from the console can be received in the java program and it can be used as an input. class CommandLineExample{ public static void main(String args[]) { System.out.println("Your first argument is: "+args[0]); } }