SlideShare a Scribd company logo
1
Software Construction
Lab 4
Classes and Objects in Java
Object Oriented Design
Implementation in Java
2
Contents
 Implementing classes and objects in Java.
 Defining different variables, constructors and other
methods
 Understand how some of the OO concepts learnt so
far are supported in Java.
 Implementing OO concepts in java.
3
Defining a Constructor: Example
public class Counter {
int CounterIndex;
// Constructor
public Counter() {}
public Counter(int x) {
CounterIndex = x;
}
//Methods to update or access counter
public void increase()
{
// CounterIndex = CounterIndex + 1;
CounterIndex++;
}
public void decrease()
{
CounterIndex = CounterIndex - 1;
}
int getCounterIndex()
{
return CounterIndex;
}
}
4
Trace counter value at each
statement and What is the output ?
class MyClass {
public static void main(String args[])
{
Counter counter1 = new Counter(1);
counter1.increase();
int a = counter1.getCounterIndex();
counter1.increase();
int b = counter1.getCounterIndex();
if ( a > b )
counter1.increase();
else
counter1.decrease();
System.out.println(counter1.getCounterIndex());
}
}
5
A Counter with User Supplied Initial
Value ?
 This can be done by adding another
constructor method to the class.
public class Counter {
int CounterIndex;
// Constructor 1
public Counter()
{
CounterIndex = 0;
}
public Counter(int InitValue )
{
CounterIndex = InitValue;
}
}
// A New User Class: Utilising both constructors
Counter counter1 = new Counter();
Counter counter2 = new Counter (10);
6
Adding a Multiple-Parameters
Constructor to our Circle Class
public class Circle {
public double x,y,r;
// Constructor
public Circle(double centreX, double centreY,
double radius)
{
x = centreX;
y = centreY;
r = radius;
}
//Methods to return circumference and area
public double circumference() { return 2*3.14*r; }
public double area() { return 3.14 * r * r; }
}
7
Constructors initialise Objects
 Recall the following OLD Code
Segment:
Circle aCircle = new Circle();
aCircle.x = 10.0; // initialize center and radius
aCircle.y = 20.0
aCircle.r = 5.0;
aCircle = new Circle() ;
At creation time the center and
radius are not defined.
These values are explicitly set
later.
8
Constructors initialise Objects
 With defined constructor
Circle aCircle = new Circle(10.0, 20.0, 5.0);
aCircle = new Circle(10.0, 20.0, 5.0) ;
aCircle is created with center (10, 20)
and radius 5
9
Multiple Constructors
 Sometimes want to initialize in a
number of different ways, depending
on circumstance.
 This can be supported by having
multiple constructors having different
input arguments.
10
Multiple Constructors
public class Circle {
public double x,y,r; //instance variables
// Constructors
public Circle(double centreX, double cenreY, double
radius) {
x = centreX; y = centreY; r = radius;
}
public Circle(double radius) { x=0; y=0; r = radius; }
public Circle() { x=0; y=0; r=1.0; }
//Methods to return circumference and area
public double circumference() { return 2*3.14*r; }
public double area() { return 3.14 * r * r; }
}
11
Initializing with constructors
public class TestCircles {
public static void main(String args[]){
Circle circleA = new Circle( 10.0, 12.0,
20.0);
Circle circleB = new Circle(10.0);
Circle circleC = new Circle();
}
}
circleA = new Circle(10, 12, 20) circleB = new Circle(10)
Centre = (0,0)
Radius=10
circleC = new Circle()
Centre = (0,0)
Radius = 1
Centre = (10,12)
Radius = 20
12
Method Overloading
 Constructors all have the same name.
 Methods are distinguished by their
signature:
 name
 number of arguments
 type of arguments
 position of arguments
 That means, a class can also have multiple
usual methods with the same name.
 Not to confuse with method overriding
(coming up), method overloading:
13
Polymorphism
 Allows a single method or operator
associated with different meaning
depending on the type of data passed to it. It
can be realised through:
 Method Overloading
 Operator Overloading (Supported in C++, but not
in Java)
 Defining the same method with different
argument types (method overloading) -
polymorphism.
 The method body can have different logic
depending on the date type of arguments.
14
Scenario
 A Program needs to find a maximum of two
numbers or Strings. Write a separate
function for each operation.

In C:

int max_int(int a, int b)

int max_string(char *s1, char *s2)

max_int (10, 5) or max_string (“melbourne”, “sydney”)

In Java:

int max(int a, int b)

int max(String s1, String s2)

max(10, 5) or max(“melbourne”, “sydney”)

Which is better ? Readability and intuitive wise ?
15
A Program with Method
Overloading
// Compare.java: a class comparing
different items
class Compare {
static int max(int a, int b)
{
if( a > b)
return a;
else
return b;
}
static String max(String a, String b)
{
if( a.compareTo (b) > 0)
return a;
else
return b;
}
public static void main(String args[])
{
String s1 = “Islamabad";
String s2 = “Lahore";
String s3 = “Karachi";
int a = 10;
int b = 20;
System.out.println(max(a, b));
System.out.println(max(s1, s2));
System.out.println(max(s1, s3)); }
}
16
The New this keyword
 this keyword can be used to refer to the object
itself.
It is generally used for accessing class members
(from its own methods) when they have the same
name as those passed as arguments.
public class Circle {
public double x,y,r;
// Constructor
public Circle (double x, double y, double r) {
this.x = x;
this.y = y;
this.r = r;
}
//Methods to return circumference and area
}
17
Static Members
 Java supports definition of global methods and
variables that can be accessed without creating
objects of a class. Such members are called Static
members.
 Define a variable by marking with the static
methods.
 This feature is useful when we want to create a
variable common to all instances of a class.
 One of the most common example is to have a
variable that could keep a count of how many
objects of a class have been created.
 Note: Java creates only one copy for a static variable
which can be used even if the class is never
instantiated.
18
Static Variables
 Define using static:
 Access with the class name
(ClassName.StatVarName):
public class Circle {
// class variable, one for the Circle class, how many circles
public static int numCircles;
//instance variables,one for each instance of a Circle
public double x,y,r;
// Constructors...
}
nCircles = Circle.numCircles;
19
Static Variables - Example
 Using static variables:
public class Circle {
// class variable, one for the Circle class, how many circles
private static int numCircles = 0;
private double x,y,r;
// Constructors...
Circle (double x, double y, double r){
this.x = x;
this.y = y;
this.r = r;
numCircles++;
}
}
20
Class Variables - Example
 Using static variables:
public class CountCircles {
public static void main(String args[]){
Circle circleA = new Circle( 10, 12, 20); //
numCircles = 1
Circle circleB = new Circle( 5, 3, 10); //
numCircles = 2
}
}
circleA = new Circle(10, 12, 20) circleB = new Circle(5, 3, 10)
numCircles
Accessing variables from another class - Example
class AccessVariableFromAnotherClass{
//Instance variable
int a = 10;
//Instance constant
final int c = 30;
//Class variable shared to all objects
static int b = 20;
}
21
Accessing variables from another class - Example
public class MainMethodClassForAcessVariable{
public static void main(String[] args){
AccessVariableFromAnotherClass avfac = new AccessVariableFromAnotherClass();
/* Instance variable can access with object */
System.out.println("Access Instance Variable like this :"+avfac.a);
/* we can access static variable from two types */
/* 1. we can access static variable with the help of object */
System.out.println("Access Static Variable like this :" +" " +avfac.b);
/* 2. we can access static variable with the help of classname */
System.out.println("Access Static Variable like this :" +" " +AccessVariableFromAnotherClass.b);
/* we cannot access static variable directly */
//System.out.println(b);
/* we can access final variable with object */
System.out.println("Access Final Variable like this :" +" " +avfac.c);
/* we cannot access final variable directly */
//System.out.println(c);
}
}
22
23
Instance Vs Static Variables
 Instance variables : One copy per
object. Every object has its own
instance variable.
 E.g. x, y, r (centre and radius in the circle)
 Static variables : One copy per class.
 E.g. numCircles (total number of circle
objects created)
24
Static Methods
 A class can have methods that are defined as
static (e.g., main method).
 Static methods can be accessed without
using objects. Also, there is NO need to
create objects.
 They are prefixed with keyword “static”
 Static methods are generally used to group
related library functions that don’t depend
on data members of its class. For example,
Math library functions.
25
Comparator class with Static
methods
// Comparator.java: A class with static data items comparision methods
class Comparator {
public static int max(int a, int b)
{
if( a > b)
return a;
else
return b;
}
public static String max(String a, String b)
{
if( a.compareTo (b) > 0)
return a;
else
return b;
}
}
class MyClass {
public static void main(String args[])
{
String s1 = "Melbourne";
String s2 = "Sydney";
String s3 = "Adelaide";
int a = 10;
int b = 20;
System.out.println(Comparator.max(a, b)); // which number is big
System.out.println(Comparator.max(s1, s2)); // which city is big
System.out.println(Comparator.max(s1, s3)); // which city is big
}
}
Directly accessed using ClassName (NO Objects)
26
Static methods restrictions
 They can only call other static methods.
 They can only access static data.
 They cannot refer to “this” or “super”
(more later) in anyway.
27
Summary
 Constructors allow seamless initialization of
objects.
 Classes can have multiple methods with the
same name [Overloading]
 Classes can have static members, which
serve as global members of all objects of a
class.
 Keywords: constructors, polymorphism,
method overloading, this, static variables,
static methods.
28
Summary
 Classes, objects, and methods are the basic
components used in Java programming.
 We have discussed:
 How to define a class
 How to create objects
 How to add data fields and methods to classes
 How to access data fields and methods to classes

More Related Content

PPT
Unit 1 Part - 3 constructor Overloading Static.ppt
PPT
Object and class
PPTX
Class and Object.pptx
PPT
packages and interfaces
PPT
Core Java unit no. 1 object and class ppt
PPT
Module 3 Class and Object.ppt
PPT
Class and Object.ppt
PPT
Lect 1-class and object
Unit 1 Part - 3 constructor Overloading Static.ppt
Object and class
Class and Object.pptx
packages and interfaces
Core Java unit no. 1 object and class ppt
Module 3 Class and Object.ppt
Class and Object.ppt
Lect 1-class and object

Similar to Lab4-Software-Construction-BSSE5.pptx ppt (20)

PPT
Core java concepts
PPT
Core Java
PPT
PPT
Object and class in java
PDF
Lecture-03 _Java Classes_from FAST-NUCES
PPT
Class & Object - Intro
PPT
Core Java Concepts
PPT
Java Presentation.ppt
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPT
Java Concepts
PPT
Java sem i
PPT
Unit 1 Part - 2 Class Object.ppt
PPT
Oops concept in c#
PPT
Java tutorial for Beginners and Entry Level
PPTX
Java Classes
PPT
Lecture 2 classes i
PPTX
Chap-2 Classes & Methods.pptx
PPT
Java class
PPTX
Java basics
PDF
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
Core java concepts
Core Java
Object and class in java
Lecture-03 _Java Classes_from FAST-NUCES
Class & Object - Intro
Core Java Concepts
Java Presentation.ppt
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Java Concepts
Java sem i
Unit 1 Part - 2 Class Object.ppt
Oops concept in c#
Java tutorial for Beginners and Entry Level
Java Classes
Lecture 2 classes i
Chap-2 Classes & Methods.pptx
Java class
Java basics
Session 3 Constructors, Types, Overloading, Static MethodsNotes.pdf
Ad

More from MuhammadAbubakar114879 (20)

PPT
Lecture 11.pptHuman Computer Interaction
PPT
3 only package diagram till slide no. 13.ppt
PPT
4 Activity & Statechart diagram.ppt4 Activity & Statechart diagram.ppt4 Activ...
PPT
6 Design Pattern.ppt design pattern in softeare engineering
PPTX
9 Component Based SE.pptx9 Component Based SE.pptx9 Component Based SE.pptx
PPTX
8 SOA.pptx soa ppt in software engineering
PPTX
7 SPL.pptx spl ppt in software engineering
PPT
Lect_07_Use Case Diagram.ppt use case diagram
PPTX
Lecture3 - Methodologies - Software-Construction-BSSE5 (1).pptx
PPTX
Lecture-7.pptx software design and Arthitechure
PPTX
design_pattern.pptx design_pattern design_pattern
PPTX
10- Architectureign Design_designnn.pptx
PPTX
13- Architecture Evaluations_design.pptx
PPTX
5-Oject Design & Mapping on Code__ .pptx
PPTX
6-User Interface Design_6-User Interface Design.pptx
PPTX
3-System Design_software_design_algo .pptx
PPTX
Sequence_Diagram_software_Design_algo.pptx
PPTX
lec 14-15 Jquery_All About J-query_.pptx
PPTX
Natural_language_processingusing python.pptx
PPTX
SPM presentation extra material-Lect 9and 10.pptx
Lecture 11.pptHuman Computer Interaction
3 only package diagram till slide no. 13.ppt
4 Activity & Statechart diagram.ppt4 Activity & Statechart diagram.ppt4 Activ...
6 Design Pattern.ppt design pattern in softeare engineering
9 Component Based SE.pptx9 Component Based SE.pptx9 Component Based SE.pptx
8 SOA.pptx soa ppt in software engineering
7 SPL.pptx spl ppt in software engineering
Lect_07_Use Case Diagram.ppt use case diagram
Lecture3 - Methodologies - Software-Construction-BSSE5 (1).pptx
Lecture-7.pptx software design and Arthitechure
design_pattern.pptx design_pattern design_pattern
10- Architectureign Design_designnn.pptx
13- Architecture Evaluations_design.pptx
5-Oject Design & Mapping on Code__ .pptx
6-User Interface Design_6-User Interface Design.pptx
3-System Design_software_design_algo .pptx
Sequence_Diagram_software_Design_algo.pptx
lec 14-15 Jquery_All About J-query_.pptx
Natural_language_processingusing python.pptx
SPM presentation extra material-Lect 9and 10.pptx
Ad

Recently uploaded (20)

PDF
RMMM.pdf make it easy to upload and study
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
Cell Structure & Organelles in detailed.
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PDF
Basic Mud Logging Guide for educational purpose
PPTX
Pharma ospi slides which help in ospi learning
PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PDF
Pre independence Education in Inndia.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PDF
Insiders guide to clinical Medicine.pdf
PPTX
Institutional Correction lecture only . . .
PDF
01-Introduction-to-Information-Management.pdf
PPTX
GDM (1) (1).pptx small presentation for students
PPTX
Final Presentation General Medicine 03-08-2024.pptx
RMMM.pdf make it easy to upload and study
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
Cell Structure & Organelles in detailed.
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
STATICS OF THE RIGID BODIES Hibbelers.pdf
Basic Mud Logging Guide for educational purpose
Pharma ospi slides which help in ospi learning
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
Pre independence Education in Inndia.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Microbial diseases, their pathogenesis and prophylaxis
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
Abdominal Access Techniques with Prof. Dr. R K Mishra
Insiders guide to clinical Medicine.pdf
Institutional Correction lecture only . . .
01-Introduction-to-Information-Management.pdf
GDM (1) (1).pptx small presentation for students
Final Presentation General Medicine 03-08-2024.pptx

Lab4-Software-Construction-BSSE5.pptx ppt

  • 1. 1 Software Construction Lab 4 Classes and Objects in Java Object Oriented Design Implementation in Java
  • 2. 2 Contents  Implementing classes and objects in Java.  Defining different variables, constructors and other methods  Understand how some of the OO concepts learnt so far are supported in Java.  Implementing OO concepts in java.
  • 3. 3 Defining a Constructor: Example public class Counter { int CounterIndex; // Constructor public Counter() {} public Counter(int x) { CounterIndex = x; } //Methods to update or access counter public void increase() { // CounterIndex = CounterIndex + 1; CounterIndex++; } public void decrease() { CounterIndex = CounterIndex - 1; } int getCounterIndex() { return CounterIndex; } }
  • 4. 4 Trace counter value at each statement and What is the output ? class MyClass { public static void main(String args[]) { Counter counter1 = new Counter(1); counter1.increase(); int a = counter1.getCounterIndex(); counter1.increase(); int b = counter1.getCounterIndex(); if ( a > b ) counter1.increase(); else counter1.decrease(); System.out.println(counter1.getCounterIndex()); } }
  • 5. 5 A Counter with User Supplied Initial Value ?  This can be done by adding another constructor method to the class. public class Counter { int CounterIndex; // Constructor 1 public Counter() { CounterIndex = 0; } public Counter(int InitValue ) { CounterIndex = InitValue; } } // A New User Class: Utilising both constructors Counter counter1 = new Counter(); Counter counter2 = new Counter (10);
  • 6. 6 Adding a Multiple-Parameters Constructor to our Circle Class public class Circle { public double x,y,r; // Constructor public Circle(double centreX, double centreY, double radius) { x = centreX; y = centreY; r = radius; } //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; } }
  • 7. 7 Constructors initialise Objects  Recall the following OLD Code Segment: Circle aCircle = new Circle(); aCircle.x = 10.0; // initialize center and radius aCircle.y = 20.0 aCircle.r = 5.0; aCircle = new Circle() ; At creation time the center and radius are not defined. These values are explicitly set later.
  • 8. 8 Constructors initialise Objects  With defined constructor Circle aCircle = new Circle(10.0, 20.0, 5.0); aCircle = new Circle(10.0, 20.0, 5.0) ; aCircle is created with center (10, 20) and radius 5
  • 9. 9 Multiple Constructors  Sometimes want to initialize in a number of different ways, depending on circumstance.  This can be supported by having multiple constructors having different input arguments.
  • 10. 10 Multiple Constructors public class Circle { public double x,y,r; //instance variables // Constructors public Circle(double centreX, double cenreY, double radius) { x = centreX; y = centreY; r = radius; } public Circle(double radius) { x=0; y=0; r = radius; } public Circle() { x=0; y=0; r=1.0; } //Methods to return circumference and area public double circumference() { return 2*3.14*r; } public double area() { return 3.14 * r * r; } }
  • 11. 11 Initializing with constructors public class TestCircles { public static void main(String args[]){ Circle circleA = new Circle( 10.0, 12.0, 20.0); Circle circleB = new Circle(10.0); Circle circleC = new Circle(); } } circleA = new Circle(10, 12, 20) circleB = new Circle(10) Centre = (0,0) Radius=10 circleC = new Circle() Centre = (0,0) Radius = 1 Centre = (10,12) Radius = 20
  • 12. 12 Method Overloading  Constructors all have the same name.  Methods are distinguished by their signature:  name  number of arguments  type of arguments  position of arguments  That means, a class can also have multiple usual methods with the same name.  Not to confuse with method overriding (coming up), method overloading:
  • 13. 13 Polymorphism  Allows a single method or operator associated with different meaning depending on the type of data passed to it. It can be realised through:  Method Overloading  Operator Overloading (Supported in C++, but not in Java)  Defining the same method with different argument types (method overloading) - polymorphism.  The method body can have different logic depending on the date type of arguments.
  • 14. 14 Scenario  A Program needs to find a maximum of two numbers or Strings. Write a separate function for each operation.  In C:  int max_int(int a, int b)  int max_string(char *s1, char *s2)  max_int (10, 5) or max_string (“melbourne”, “sydney”)  In Java:  int max(int a, int b)  int max(String s1, String s2)  max(10, 5) or max(“melbourne”, “sydney”)  Which is better ? Readability and intuitive wise ?
  • 15. 15 A Program with Method Overloading // Compare.java: a class comparing different items class Compare { static int max(int a, int b) { if( a > b) return a; else return b; } static String max(String a, String b) { if( a.compareTo (b) > 0) return a; else return b; } public static void main(String args[]) { String s1 = “Islamabad"; String s2 = “Lahore"; String s3 = “Karachi"; int a = 10; int b = 20; System.out.println(max(a, b)); System.out.println(max(s1, s2)); System.out.println(max(s1, s3)); } }
  • 16. 16 The New this keyword  this keyword can be used to refer to the object itself. It is generally used for accessing class members (from its own methods) when they have the same name as those passed as arguments. public class Circle { public double x,y,r; // Constructor public Circle (double x, double y, double r) { this.x = x; this.y = y; this.r = r; } //Methods to return circumference and area }
  • 17. 17 Static Members  Java supports definition of global methods and variables that can be accessed without creating objects of a class. Such members are called Static members.  Define a variable by marking with the static methods.  This feature is useful when we want to create a variable common to all instances of a class.  One of the most common example is to have a variable that could keep a count of how many objects of a class have been created.  Note: Java creates only one copy for a static variable which can be used even if the class is never instantiated.
  • 18. 18 Static Variables  Define using static:  Access with the class name (ClassName.StatVarName): public class Circle { // class variable, one for the Circle class, how many circles public static int numCircles; //instance variables,one for each instance of a Circle public double x,y,r; // Constructors... } nCircles = Circle.numCircles;
  • 19. 19 Static Variables - Example  Using static variables: public class Circle { // class variable, one for the Circle class, how many circles private static int numCircles = 0; private double x,y,r; // Constructors... Circle (double x, double y, double r){ this.x = x; this.y = y; this.r = r; numCircles++; } }
  • 20. 20 Class Variables - Example  Using static variables: public class CountCircles { public static void main(String args[]){ Circle circleA = new Circle( 10, 12, 20); // numCircles = 1 Circle circleB = new Circle( 5, 3, 10); // numCircles = 2 } } circleA = new Circle(10, 12, 20) circleB = new Circle(5, 3, 10) numCircles
  • 21. Accessing variables from another class - Example class AccessVariableFromAnotherClass{ //Instance variable int a = 10; //Instance constant final int c = 30; //Class variable shared to all objects static int b = 20; } 21
  • 22. Accessing variables from another class - Example public class MainMethodClassForAcessVariable{ public static void main(String[] args){ AccessVariableFromAnotherClass avfac = new AccessVariableFromAnotherClass(); /* Instance variable can access with object */ System.out.println("Access Instance Variable like this :"+avfac.a); /* we can access static variable from two types */ /* 1. we can access static variable with the help of object */ System.out.println("Access Static Variable like this :" +" " +avfac.b); /* 2. we can access static variable with the help of classname */ System.out.println("Access Static Variable like this :" +" " +AccessVariableFromAnotherClass.b); /* we cannot access static variable directly */ //System.out.println(b); /* we can access final variable with object */ System.out.println("Access Final Variable like this :" +" " +avfac.c); /* we cannot access final variable directly */ //System.out.println(c); } } 22
  • 23. 23 Instance Vs Static Variables  Instance variables : One copy per object. Every object has its own instance variable.  E.g. x, y, r (centre and radius in the circle)  Static variables : One copy per class.  E.g. numCircles (total number of circle objects created)
  • 24. 24 Static Methods  A class can have methods that are defined as static (e.g., main method).  Static methods can be accessed without using objects. Also, there is NO need to create objects.  They are prefixed with keyword “static”  Static methods are generally used to group related library functions that don’t depend on data members of its class. For example, Math library functions.
  • 25. 25 Comparator class with Static methods // Comparator.java: A class with static data items comparision methods class Comparator { public static int max(int a, int b) { if( a > b) return a; else return b; } public static String max(String a, String b) { if( a.compareTo (b) > 0) return a; else return b; } } class MyClass { public static void main(String args[]) { String s1 = "Melbourne"; String s2 = "Sydney"; String s3 = "Adelaide"; int a = 10; int b = 20; System.out.println(Comparator.max(a, b)); // which number is big System.out.println(Comparator.max(s1, s2)); // which city is big System.out.println(Comparator.max(s1, s3)); // which city is big } } Directly accessed using ClassName (NO Objects)
  • 26. 26 Static methods restrictions  They can only call other static methods.  They can only access static data.  They cannot refer to “this” or “super” (more later) in anyway.
  • 27. 27 Summary  Constructors allow seamless initialization of objects.  Classes can have multiple methods with the same name [Overloading]  Classes can have static members, which serve as global members of all objects of a class.  Keywords: constructors, polymorphism, method overloading, this, static variables, static methods.
  • 28. 28 Summary  Classes, objects, and methods are the basic components used in Java programming.  We have discussed:  How to define a class  How to create objects  How to add data fields and methods to classes  How to access data fields and methods to classes