Simple class and object examples in java
-
Upload
harish-gyanani -
Category
Education
-
view
829 -
download
3
description
Transcript of Simple class and object examples in java
![Page 1: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/1.jpg)
Simple class and object examples in Java
Presented By Harish Gyanani
![Page 2: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/2.jpg)
What is class?
A class is a blue print from which individual objects are created.
![Page 3: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/3.jpg)
Class naming convention
• By convention, class names capitalize the initial of each word.
• For example: Employee, Boss, DateUtility, PostOffice, RegularRateCalculator.
![Page 4: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/4.jpg)
What are members of Class?
• Field: – field is nothing but the property of the class or object which
we are going to create .– Example if we are creating a class called computer then they
have property like model, memSize, hdSize, osType etc.
• Method:– method is nothing but the operation that an object can
perform it define the behavior of object how an object can interact with outside world.
– Example startMethod (), shutdownMethod ().
![Page 5: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/5.jpg)
Fields in class1.Fields are variables.2.They can be primitives or references to objects.For example, the Employee class has two fields, age and salary.
public class Employee{ int age; int salary}
![Page 6: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/6.jpg)
Fields Naming Conventions
![Page 7: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/7.jpg)
Fields Naming Conventions
1.Field names should follow the camel naming convention.
![Page 8: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/8.jpg)
Fields Naming Conventions
1.Field names should follow the camel naming convention.
2.The initial of each word in the field, except for the first word, is written with a capital letter.
![Page 9: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/9.jpg)
Fields Naming Conventions
1.Field names should follow the camel naming convention.
2.The initial of each word in the field, except for the first word, is written with a capital letter.
3.For example: age, maxAge, address, validAddress, numberOfRows.
![Page 10: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/10.jpg)
Instance variables
Variables within a class but outside any method.
![Page 11: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/11.jpg)
Instance Methods
Methods defined in a class which is only accessible through the Object of the class are called Instance methods.
![Page 12: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/12.jpg)
Example: Person Class
class Person{ String name; int age;}
![Page 13: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/13.jpg)
Example: Person Class
class Person{ String name; int age;}
Without methods
![Page 14: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/14.jpg)
Example: Person Classclass keyword
class Person{ String name; int age;}
Without methods
![Page 15: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/15.jpg)
Example: Person ClassName of class
class keyword
class Person{ String name; int age;}
Without methods
![Page 16: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/16.jpg)
Example: Person ClassName of class
class keyword
Start of class
class Person{ String name; int age;}
Without methods
![Page 17: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/17.jpg)
Example: Person ClassName of class
class keyword
Start of class
End of class
class Person{ String name; int age;}
Without methods
![Page 18: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/18.jpg)
Example: Person ClassName of class
class keyword
Start of classData members of class
with default access(instance variable)End of
class
class Person{ String name; int age;}
Without methods
![Page 19: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/19.jpg)
How to declare object?
![Page 20: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/20.jpg)
How to declare object?
Person p1;
![Page 21: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/21.jpg)
How to declare object?
Person p1;
//declare reference to object
![Page 22: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/22.jpg)
How to declare object?
Person p1;
//declare reference to object//Syntax: <classname> <objectname>
![Page 23: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/23.jpg)
How to declare object?
Person p1;
//declare reference to object//Syntax: <classname> <objectname>
• It is simply a variable that can refer to an object.
![Page 24: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/24.jpg)
How to declare object?
Person p1;
//declare reference to object//Syntax: <classname> <objectname>
• It is simply a variable that can refer to an object.
nullp1
Person p1;
![Page 25: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/25.jpg)
Allocate memory
![Page 26: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/26.jpg)
Allocate memory
p1 = new Person();
![Page 27: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/27.jpg)
Allocate memory
p1 = new Person();
//allocate a Person object
![Page 28: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/28.jpg)
Allocate memory
p1 = new Person();
//allocate a Person object//Syntax: <objectname> = new <classname>();
![Page 29: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/29.jpg)
Allocate memory
p1 = new Person();
//allocate a Person object//Syntax: <objectname> = new <classname>();
• The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it.
![Page 30: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/30.jpg)
Allocate memory
p1 = new Person();
//allocate a Person object//Syntax: <objectname> = new <classname>();
• The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it.
• This reference is, the address in memory of the object allocated by new.
![Page 31: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/31.jpg)
Allocate memory
p1 = new Person();
//allocate a Person object//Syntax: <objectname> = new <classname>();
• The new operator dynamically allocates (that is, allocates at run time) memory for an object and returns a reference to it.
• This reference is, the address in memory of the object allocated by new.
p1 = new Person(); p1
name
age
Person object
![Page 32: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/32.jpg)
Combination of these statements
![Page 33: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/33.jpg)
Combination of these statements
![Page 34: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/34.jpg)
Combination of these statements
![Page 35: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/35.jpg)
Combination of these statements
![Page 36: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/36.jpg)
Person p1 = new Person();
Combination of these statements
![Page 37: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/37.jpg)
Person p1 = new Person();
Combination of these statements
//Syntax: <classname> <objectname> = new <classname>();
![Page 38: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/38.jpg)
public class for Person class public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;
int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}
![Page 39: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/39.jpg)
public class for Person class public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;
int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}
Instance variables are initialized with
object name qualifier
![Page 40: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/40.jpg)
public class for Person class public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;
int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}
Instance variables are initialized with
object name qualifier
Syntax to set value in instance variable:-
<objectname>.<variablename> = <value>;
![Page 41: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/41.jpg)
public class for Person class
Syntax to get value from instance
variable:-<variable> = <objectname>.<instance_variable_name>
public class NewClass1{ public static void main(String args[]) { Person obj1 = new Person(); obj1.name=“ramesh"; obj1.age=22;
int a=obj1.age; System.out.println(a); System.out.println(obj1.name); }}
Instance variables are initialized with
object name qualifier
Syntax to set value in instance variable:-
<objectname>.<variablename> = <value>;
![Page 42: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/42.jpg)
Complete Programpublic class NewClass1{ public static void main(String args[]) {
Person obj1 = new Person();obj1.name=“ramesh";obj1.age=22;int a=obj1.age;System.out.println(a);System.out.println(obj1.name);
}}
class Person{ String name; int age;}
![Page 43: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/43.jpg)
Output
![Page 44: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/44.jpg)
In this example, barking(), hungry() and sleeping() are instance
methods.
Instance variables
Example 1: Dog Classclass Dog{
String breed;int age;String color;
void barking() {}void hungry() {}void sleeping() {}
}
![Page 45: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/45.jpg)
Example 2: Stock class
Class Stock {
public commodity;public price;
public void buy (int no_of commodity) {}public boolean sale () {}
}
![Page 46: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/46.jpg)
Instance variables
Example 2: Stock class
Class Stock {
public commodity;public price;
public void buy (int no_of commodity) {}public boolean sale () {}
}
![Page 47: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/47.jpg)
In this example, buy(), and sale() are instance
methods.
Instance variables
Example 2: Stock class
Class Stock {
public commodity;public price;
public void buy (int no_of commodity) {}public boolean sale () {}
}
![Page 48: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/48.jpg)
In this example, buy(), and sale() are instance
methods.
Instance variables
Example 2: Stock class
Class Stock {
public commodity;public price;
public void buy (int no_of commodity) {}public boolean sale () {}
}
Collectively, the methods and variables defined within a class arecalled members of the class.
![Page 49: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/49.jpg)
Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
![Page 50: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/50.jpg)
Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
With methods
![Page 51: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/51.jpg)
Private instance variables cannot be accessed outside the class
Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
With methods
![Page 52: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/52.jpg)
Private instance variables cannot be accessed outside the class
getData() and display() instance
methods are public and can be accessed outside
the class.
Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
With methods
![Page 53: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/53.jpg)
Private instance variables cannot be accessed outside the class
getData() and display() instance
methods are public and can be accessed outside
the class.
Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
Methods inside class can access private data of class. In this case getData() and display() methods
are accessing private data.
With methods
![Page 54: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/54.jpg)
Private instance variables cannot be accessed outside the class
getData() and display() instance
methods are public and can be accessed outside
the class.
Example 3: Person Classclass Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
Methods inside class can access private data of class. In this case getData() and display() methods
are accessing private data.
They are defined inside
class not inside method
With methods
![Page 55: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/55.jpg)
public class code for Person class
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData();
p1.display(); }}
NOTE: getData() and Display()
method cannot be called without
object qualifier.
![Page 56: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/56.jpg)
public class code for Person class
Qualifier: Object name
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData();
p1.display(); }}
NOTE: getData() and Display()
method cannot be called without
object qualifier.
![Page 57: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/57.jpg)
public class code for Person class
Dot operator
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData();
p1.display(); }}
NOTE: getData() and Display()
method cannot be called without
object qualifier.
![Page 58: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/58.jpg)
public class code for Person class
Instance method because it is called
using object
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData();
p1.display(); }}
NOTE: getData() and Display()
method cannot be called without
object qualifier.
![Page 59: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/59.jpg)
public class code for Person class
Instance method because it is called
using object
Dot operator
Qualifier: Object name
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData();
p1.display(); }}
NOTE: getData() and Display()
method cannot be called without
object qualifier.
![Page 60: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/60.jpg)
Complete programimport java.util.Scanner;
class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData(); p1.display(); }}
![Page 61: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/61.jpg)
A program can contain multiple classes but only one public
class(same name as file name) and contains main method
Complete programimport java.util.Scanner;
class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData(); p1.display(); }}
![Page 62: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/62.jpg)
Creating and instantiating Person class object p1.
A program can contain multiple classes but only one public
class(same name as file name) and contains main method
Complete programimport java.util.Scanner;
class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData(); p1.display(); }}
![Page 63: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/63.jpg)
Calling instance methods of
Person Class using p1 object.
Creating and instantiating Person class object p1.
A program can contain multiple classes but only one public
class(same name as file name) and contains main method
Complete programimport java.util.Scanner;
class Person{ private String name; private int age; public void getData() { Scanner sc = new Scanner(System.in); System.out.println("Enter name and age"); name=sc.nextLine(); age=sc.nextInt(); } public void display() { System.out.println("Name ="+name); System.out.println("Age ="+age); }}
public class abc { public static void main(String args[]) { Person p1 = new Person();
p1.getData(); p1.display(); }}
![Page 64: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/64.jpg)
Output
![Page 65: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/65.jpg)
Members of class
![Page 66: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/66.jpg)
Members of class
Members of class
![Page 67: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/67.jpg)
Members of class
Members of class
Data members
![Page 68: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/68.jpg)
Members of class
Members of class
Data members Methods
![Page 69: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/69.jpg)
Members of class
Members of class
Data members
Instance data members
Methods
![Page 70: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/70.jpg)
Members of class
Members of class
Data members
Instance data members
Static data members/
Class Variables
Methods
![Page 71: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/71.jpg)
Members of class
Members of class
Data members
Instance data members
Static data members
Methods
Instance methods
![Page 72: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/72.jpg)
Members of class
Members of class
Data members
Instance data members
Static data members
Methods
Instance methods
Static methods/
Class methods
![Page 73: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/73.jpg)
Class diagram in UML
![Page 74: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/74.jpg)
Class diagram in UML
UML class is represented by the diagram shown below. The diagram is divided into four parts:-
![Page 75: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/75.jpg)
Class diagram in UML
UML class is represented by the diagram shown below. The diagram is divided into four parts:-
•The top section is used to name the class.
![Page 76: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/76.jpg)
Class diagram in UML
UML class is represented by the diagram shown below. The diagram is divided into four parts:-
•The top section is used to name the class.
•The second one is used to show the attributes of the class.
![Page 77: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/77.jpg)
Class diagram in UML
UML class is represented by the diagram shown below. The diagram is divided into four parts:-
•The top section is used to name the class.
•The second one is used to show the attributes of the class.
•The third section is used to describe the operations performed by the class.
![Page 78: Simple class and object examples in java](https://reader033.fdocuments.us/reader033/viewer/2022061218/54b62a554a795930308b4576/html5/thumbnails/78.jpg)
Variable Types
A class can contain any of the following variable types.• Local variables: Variables defined inside methods,
constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.
• Instance variables: Instance variables are variables within a class but outside any method. These variables are instantiated when the class is loaded. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.
• Class variables: Class variables are variables declared with in a class, outside any method, with the static keyword.