0% found this document useful (0 votes)
7 views

Java Lesson 4

This document discusses class constants, local variables, and the rules for identifiers in Java programming. It provides an example of using constants, explains what local variables are and how they differ from parameters and data members, and summarizes the key points about constants and local variables.

Uploaded by

kaung71669
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

Java Lesson 4

This document discusses class constants, local variables, and the rules for identifiers in Java programming. It provides an example of using constants, explains what local variables are and how they differ from parameters and data members, and summarizes the key points about constants and local variables.

Uploaded by

kaung71669
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 9

Java Programming: Class Constants

and Local Variables


Dr. May Phyo Thu
Lecturer

https://blogs.ashrithgn.com
Objectives

• To understand the usage of constants for the class

• To know the declaration of local variables

• To learn the rules related with identifiers

Faculty of Computer Science


Class Constants

• The use of constants

• provides a meaningful description of what the values stand for, for example,
number = UNDEFINED; is more meaningful than number = -1
• provides easier program maintenance

• only need to change the value in the constant declaration instead of locating all
occurrences of the same value in the program code

Faculty of Computer Science


A Sample Use of Constants

import java.util.Random;

class Dice {
private static final int MAX_NUMBER = 6;
private static final int MIN_NUMBER = 1;
private static final int NO_NUMBER = 0;
private int number;
private Random random;

public Dice( ) {
random = new Random();
number = NO_NUMBER;
}
Faculty of Computer Science
Cond

public void roll( ) {


number =
random.nextInt(MAX_NUMBER -
MIN_NUMBER + 1) + MIN_NUMBER;
}
public int getNumber( ) {
return number;
}
}

Faculty of Computer Science


Local Variables

• Local variables are declared within a method declaration and used for temporary
services, such as storing intermediate computation results.

public double convert(int num) {

double result; local variable

result = Math.sqrt(num * num);

return result;
}

Faculty of Computer Science


Local, Parameter & Data Member

• An identifier appearing inside a method can be a local variable, a parameter, or a


data member.
• The rules are:

• If there’s a matching local variable declaration or a parameter, then the


identifier refers to the local variable or the parameter.
• Otherwise, if there’s a matching data member declaration, then the identifier
refers to the data member.
• Otherwise, it is an error because there’s no matching declaration.

Faculty of Computer Science


Summary

• After you have studied this lesson, you should know

• how to use constants, a meaningful description of what the values stand for

• about local variables

• the rules for identifiers appearing inside a method

Faculty of Computer Science


Next Lesson

• selection control using if statement

• selection control using nested if statement

Faculty of Computer Science

You might also like