You are on page 1of 27

Attributes, methods and

constructors, characters and


String
 EXPLAIN THE DIFFERENCE
BETWEEN AN ATTRIBUTE AND
A METHOD.

 CREATE ATTRIBUTES AND


METHODS.

Objectives  CREATE CONSTRUCTORS THAT


CAN ACCEPT VALUE FROM ITS
CALLER.

 INITIALIZE ATTRIBUTES
USING CONSTRUCTORS.

 USE STRING METHODS


INDICATED IN API
DOCUMENTATION IN JAVA.
Introduction – Syntax for class and attributes

In java, a program is a collection of one or more classes. So a


very simple application program must have at least once class.
The basic syntax of class can be shown using a syntax template
as follows:
[accessModifiers] class ClassName [classModifiers]
{
[members/attributes/fields]
}

20XX presentation title 3


Introduction – Syntax for class and attributes

An item inside a pair of square brackets in a syntax


template is optional. In the template, ClassName is
a name you would like to give to the class. The
optional members consist of attributes and
operations of the class. The optional
accessModifiers determines, among other things,
the accessibility of the class to other classes and
Java virtual machine(JVM).

20XX presentation title 4


Introduction – Syntax for class and attributes

For example, the access modifier public makes a class


accessible to JVM. Because JVM needs access to your
program, every application must have one class with
public or package access. We will have more to say
about this topic later. Both public, and class are reserved
words or keywords. A reserved word is a word with
special meaning. For example, the reserved word class
indicates the beginning of a class definition.

20XX presentation title 5


Introduction – Syntax for class and attributes

In syntax template, all reserved words


are shown in blue text. The optional
classModifier will not be discussed in
this module but basically its just for
inheritance or interface.

20XX presentation title 6


Introduction – Syntax for class and attributes

Following the reserved word class, you


must supply a name for the class. Note that
the name identifies a class. Similarly, you
need to provide a name to attributes and
operations of a class. All such names are
collectively known as identifiers.

20XX presentation title 7


Introduction – Syntax for class and attributes

Example:
public class SampleClass extends SomeSuperClass
{
private String attributeName = “sample”;
private int age = 20;
private char section = ‘A’;
}

20XX presentation title 8


Introduction to Methods.

A Java program is a collection of collaborating


objects. By the term collaborating, we mean
whenever an object needs some task to be
done, it invokes a method of a class that could
provide the service. Here is an example:

System.out.println(“Hello world”);

20XX presentation title 9


Introduction to Methods.

System is a class and calls its attribute


out which is of type PrintStream
which is another class. And from the
PrintStream class, we used the
println() method to print the String
inside the double quotes.
20XX presentation title 10
Introduction to Methods.

Same goes to Scanner class, wherein we have to


create an instance of Scanner using the keyword new
in order for us to create an object of it and pass the
System.in from System class to its constructor and
use its method.

Scanner input = new Scanner(System.in);


int var1 = input.nextInt();

20XX presentation title 11


Method Definition

We’re already familiar with the syntax


template of the method main as shown below:

public static void main(String[ ] args) {


[statements]
}

20XX presentation title 12


Method Definition

Observe that the method consists of two parts: the first one is the header
or method signature:

public static void main(String[ ] args)

And then, the second is method body:

{
[Statement]
}

20XX presentation title 13


Method Definition

The header starts with the keyword


public that makes the method
accessible to other objects. If you use
the keyword private, the method
cannot be invoked by other objects.

20XX presentation title 14


Method Definition

The keyword static refers to the fact that


method main is shared by all objects
belonging to the class. For the present,
note that we define all the methods other
than the main as non-static methods and as
such the keyword static will be omitted.

20XX presentation title 15


Method Definition

The next keyword is the return type of


the method. The method main is a void
method. In a method, the correct return
type needs to be specified. After the
return type, you must provide an
identifier as the name of the method.

20XX presentation title 16


Method Definition

The naming convention for methods is identical to that


of an instance variable. The header ends with a list of
formal parameters (or arguments) that are enclosed
within a pair of parentheses. The formal parameter list
in this case is String[] args. You need to know arrays for
a full explanation. The list of formal parameters in a
method header can be empty. However, the pair of
parentheses enclosing the formal parameter cannot be
omitted.

20XX presentation title 17


Constructor in Java

A constructor is used to initialized attributes/


instance/ member variables. The syntax
template of a constructor can be thought of as
a mutator/setter method with no return type
and name the same as the name of the class.
Thus, a constructor for the Stock class can be
written as follows:

20XX presentation title 18


Constructor in Java
class Stock
{
private int numberOfShares;
private String symbol;
private firstname;
private site;

public Stock(int share, String fname) {


numberOfShares = share;
symbol = “@”;
firstname = fname;
site = “email.com”;
}
}

20XX presentation title 19


Constructor in Java

In our Stock class, we have four attributes:


numberOfShares, symbol, firstname and
email. We only initialized them and assign a
value to it once an instance of Stock is
created. We pass 2 parameters share and
fname from the caller and assign it to
numberOfShares and firstname respectively.

20XX presentation title 20


Constructor in Java

class MainClass
{
public static void main(String[ ] args) {
Stock st = new Stock(15, “John”); //creating
instance of class Stock
}
}

20XX presentation title 21


Constructor in Java

Once you created an instance, the constructor


for that class will automatically be executed by
the program. Thus, will perform the
initialization process for the attributes of Stock
class. The new keyword is used to give signal
to the compiler that the program will allocate a
memory to the object/instance of a class.

20XX presentation title 22


Characters and String

In our previous module, we enclosed


text inside a pair of double quotes to
represent a String. Like this:

“Hello! Laguna State Polytechnic


University – Sta.Cruz”
20XX presentation title 23
Characters and String

Every character has a positional value in a String has a


positional value. The positional value of the first character
in a String is 0. The positional value of the second
character is 1, and so on(including spaces). The number of
characters in a String is called the length of the String.
The length of the String above is 53 and the highest
positional value(index) is 52(starting from 0) meaning to
say, in order to compute for the highest index, we only
need to deduct 1 to the length of our String.

20XX presentation title 24


Characters and String

In java, “A” and ‘A’ are different. “A”


is a String of length 1 while ‘A’ is a
character. Notice the quotation marks
from both letters. If String has double
quotes, a character has a pair of single
quotation.
20XX presentation title 25
Characters and String

The char data type represents singe


characters. A single character can be
of any of the letters (a to z, A to Z),
digits(0 to 9) and special characters(!,
@, #, $, %, ^, &, *, {, }, [, ], and so
on)
20XX presentation title 26
thank you

You might also like