Sei sulla pagina 1di 35

Java

Classes
and
Methods
CC102- Programming Fundamentals
LESSON VI – Java Classes and Methods

LEARNING OUTCOMES:
At the end of the session, the students should be
able to:
• Identity the different class according to structure
and characteristics
• Create methods with different parameters and
arguments
• Develop code that declares classes, and includes
the appropriate use of package and import
statements.

CC102- PROGRAMMING FUNDAMENTALS 2


LESSON VI – Java Classes and Methods

What is a Java Method?


• Methods represent operations on data and
also hold the logic to determine those
operations.
• Using methods offer two main advantages:
1. A method may be executed (called) repeatedly
from different points in the program: decrease the
program size, the effort to maintain the code and the
probability for an error.

CC102- PROGRAMMING FUNDAMENTALS 3


LESSON VI – Java Classes and Methods

What is a Java Method?


• Methods represent operations on data and
also hold the logic to determine those
operations.
• Using methods offer two main advantages:
2. Methods help make the program logically
segmented, or modularized: less error prone, and
easier to maintain.

CC102- PROGRAMMING FUNDAMENTALS 4


LESSON VI – Java Classes and Methods

Defining a method
• A method is a self-contained block of code
that performs specific operations on the
data by using some logic

Method declaration:
• Name
• Parameter(s)
• Argument(s)
• Return type
• Access modifier 5
CC102- PROGRAMMING FUNDAMENTALS
LESSON VI – Java Classes and Methods

a method syntax
1 2 3 4

1. Access modifier
2. Return type
3. Name
4. Parameter(s)
5. Argument(s)

CC102- PROGRAMMING FUNDAMENTALS 6


LESSON VI – Java Classes and Methods

a method example
1 2 3 4

1. Access modifier
2. Return type
3. Name
4. Parameter(s)
5. Argument(s)
CC102- PROGRAMMING FUNDAMENTALS 7
LESSON VI – Java Classes and Methods

a method example
1 3 4

1. Access modifier
3. Name
4. Parameter(s)

CC102- PROGRAMMING FUNDAMENTALS 8


LESSON VI – Java Classes and Methods

Static methods and


variables

The static methods and variables are shared by


all the instances of a class

The static modifier may be applied to a variable, a


method, and a block of code inside a method
CC102- PROGRAMMING FUNDAMENTALS 9
LESSON VI – Java Classes and Methods

Static methods and


variables

Because a static element of a class is visible to all the


instances of the class, if one instance makes a
change to it, all the instances see that change.

CC102- PROGRAMMING FUNDAMENTALS 10


LESSON VI – Java Classes and Methods

An example of method with


return type
1 3 4

1. Access modifier
2. Return type
3. Name
4. Parameter(s)

CC102- PROGRAMMING FUNDAMENTALS 11


LESSON VI – Java Classes and Methods

An example of method
call
1 3 4

1. Access modifier
2. Return type
3. Name
4. Parameter(s)
5. Argument(s)

CC102- PROGRAMMING FUNDAMENTALS 12


LESSON VI – Java Classes and Methods

Working with Classes and


Objects
A class is a template that contains the data
variables and the methods that operate on
those data variables following some logic
Class members:
• Variables represent the state of an object
• Methods constitute its behavior

CC102- PROGRAMMING FUNDAMENTALS 13


LESSON VI – Java Classes and Methods

Defining Classes
The general syntax:
<modifier> class <className> { }
<className> specifies the name of the class

class is the keyword

<modifier> specifies some characteristics of the class:


• Access modifiers: private, protected, default and public
• Other modifiers: abstract, final, and strictfp

CC102- PROGRAMMING FUNDAMENTALS 14


LESSON VI – Java Classes and Methods

Access Modifiers Access


Levels

CC102- PROGRAMMING FUNDAMENTALS 15


LESSON VI – Java Classes and Methods

Access Modifiers Visibility


Let's look at a collection of classes and see how access levels affect
visibility. The following figure shows the four classes in this example and
how they are related.

CC102- PROGRAMMING FUNDAMENTALS 16


LESSON VI – Java Classes and Methods

Access Modifiers Visibility

CC102- PROGRAMMING FUNDAMENTALS 17


LESSON VI – Java Classes and Methods

Class Example

CC102- PROGRAMMING FUNDAMENTALS 18


LESSON VI – Java Classes and Methods

Nested Classes
• allows you to define a class (like a variable or a method)
inside a top-level class (outer class or enclosing class)
SYNTAX:

CC102- PROGRAMMING FUNDAMENTALS 19


LESSON VI – Java Classes and Methods

Nested Classes
• allows you to define a class (like a variable or a method)
inside a top-level class (outer class or enclosing class)
SYNTAX:

CC102- PROGRAMMING FUNDAMENTALS 20


LESSON VI – Java Classes and Methods

Nested Class

Example

CC102- PROGRAMMING FUNDAMENTALS 21


LESSON VI – Java Classes and Methods

Creating an Object
SYNYAX:

<variableName>: the name of the object reference that


will refer to the object that you want to create
<className>: the name of an existing class
<classConstructor>: a constructor of the class

The right side of the equation creates the object of the


class specified by <className> with the new operator,
and assigns it to <variableName>
(i.e. <variableName> points to it)
CC102- PROGRAMMING FUNDAMENTALS 22
LESSON VI – Java Classes and Methods

Example program:
public class Value{
public int InitialValueA() {
int a=5;
return a;
}
public int InitialValueB(){
int b=10;
return b;
}
}//1st class initializing the values for the 2nd class
public class compute{
public int sum(int num1,int num2){
return num1+num2;
}
}//second class with method for arithmetic operation

CC102- PROGRAMMING FUNDAMENTALS 23


LESSON VI – Java Classes and Methods

Example program:
1 public class displaysum{
2 public void display(){
3 int a,b,ValueA,ValueB;
4 //class compute and its method
5 compute addition = new compute();
6 //class value and its method
7 Value Initial = new Value();
8
9 ValueA=Initial.InitialValueA();
10 ValueB=Initial.InitialValueB();
11 a=addition.sum(ValueA,ValueB);
12 System.out.println(a);
13 System.out.println(addition.sum(10,20));
14 }
15}
//3rd class calling the 2 class and methods to perform
//the arithmetic operation with initial value and
//another sample output using the values 10 and 20

CC102- PROGRAMMING FUNDAMENTALS 24


LESSON VI – Java Classes and Methods

Example program:
public class main{
public static void main(String[] args){
displaysum print = new displaysum();
print.display();
}
}
//last class is the main class, on this class the
//output of the previous class display will be called
//as the output of the program.
//the file name of this program is main.java
// all of the 4 classes in under this source code.

CC102- PROGRAMMING FUNDAMENTALS 25


LESSON VI – Java Classes and Methods

Example program Flow


chart
main(String[] args)

display()

InitialValueA()
InitialValueB()

sum(num1,num2)

CC102- PROGRAMMING FUNDAMENTALS 26


LESSON VI – Java Classes and Methods

The static methods and


variables
• A static variable is initialized when a class is
loaded, whereas an instance variable is
initialized when an instance of the class is
created
• A static method also belongs to the class. It
can be called even before a single instance
of the class exists
• A static method can only access the static
members of the class 27
CC102- PROGRAMMING FUNDAMENTALS
LESSON VI – Java Classes and Methods

The static code block


• A class can also have a static code block
outside of any method
• The code block does not belong to any
method, but only to the class
• executed before the class is instantiated, or
even before the method main() is called

CC102- PROGRAMMING FUNDAMENTALS 28


LESSON VI – Java Classes and Methods

static code block

example

CC102- PROGRAMMING FUNDAMENTALS 29


LESSON VI – Java Classes and Methods

Methods with a variable


number parameters
The rules to define variable-length parameters:
• There must be only one variable-length parameters list.
• If there are individual parameters in addition to the list,
the variable-length parameters list must appear last
inside the parentheses of the method.
• The variable-length parameters list consists of a type
followed by three dots and the name.

CC102- PROGRAMMING FUNDAMENTALS 30


LESSON VI – Java Classes and Methods

JavaBeans Naming
Standards for methods
A JavaBean is a special kind of Java class that is
defined by following certain rules:
• The private variables / properties can only be
accessed through getter and setter methods
• The getter and setter methods must be public so
that anyone who uses the bean can invoke them.

CC102- PROGRAMMING FUNDAMENTALS 31


LESSON VI – Java Classes and Methods

JavaBeans Naming
Standards for methods
A JavaBean is a special kind of Java class that is
defined by following certain rules:
• A setter method must have the void return type
and must have a parameter that represents the
type of the corresponding property
• A getter method does not have any parameter
and its return type matches the argument type of
the corresponding setter method
CC102- PROGRAMMING FUNDAMENTALS 32
LESSON VI – Java Classes and Methods
JavaBeans Naming
Standards for methods
Examples

CC102- PROGRAMMING FUNDAMENTALS 33


LESSON VI – Java Classes and Methods

Variable number

parameters example

CC102- PROGRAMMING FUNDAMENTALS 34


THE END

CC102- Programming Fundamentals