Identifiers • Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. – Identifiers should help to document what a classes, variables, or methods are used for. – Upper and lower case letters are different in Java. • ThisOne is not the same as thisOne. – Identifiers should not include punctuation and can not include spaces.
34
Embed
Identifiers Identifiers in Java are composed of a series of letters and digits where the first character must be a letter. –Identifiers should help to.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Identifiers• Identifiers in Java are composed of a series
of letters and digits where the first character must be a letter.– Identifiers should help to document what a
classes, variables, or methods are used for.– Upper and lower case letters are different in
Java.• ThisOne is not the same as thisOne.
– Identifiers should not include punctuation and can not include spaces.
Identifiers
– A good programming standard for identifiers composed of multiple words is to capitalize the first character of the additional words.
• The case of the first letter of the first word will depend upon the use of the identifier.
• For Example,
myCar, or bobAndSusieAreFriends
Identifiers
• Class names begin with a capital letter.– This standard makes it easier to
understand which identifiers represent classes.
– In the MyFirstApplication we have two class names:
• MyFirstApplication and MainWindow
Identifiers
• Variable (object names) and method names begin with a lower case letter.– In MyFirstApplication we have one object:
• mainWindow• Note that mainWindow is different from
MainWindow.
– In MyFirstApplication we have one method:• main(…)
Creating a Java Program
• The first step to creating a Java program is to define a class containing the main method.– The MyFirstApplication program on page
39 of Wu is an example of a Java class containing a main method.
• This Java class definition can be thought of as the driver class or the ring leader class.
MyFirstApplication
MyFirstApplication
main(String[] args)
Declaring Objects
• In order to create objects to work within a Java program they must first be declared.– The object declaration includes the class
name that the object belongs to and the variable name for the object.
– The format for declaring an object is:<class name> <object(variable) name> ;
– The class must be defined before you can declare the object.
Declaring Objects
– Think about our cars. • Cars was the class name and we had three
instances of the this class.• To declare each of the cars we had created we
would type:
Cars clintsPorsche;
Cars jessicasBMW;
Cars johnsLamborghini;
Declaring Objects
• The MyFirstApplication program in Wu defines one object called mainWindow of the class type MainWindow.
MainWindow mainWindow;
– Note the differences in the text above.
Declaring Objects
• Declaring an object only creates a name to refer to the object, it does not create the instance of that object.– In order to use the named object instance,
the object must be instantiated (or created).
Object Instantiation
• Object instantiation (creation) actually creates a copy of the class and stores it in memory with the name defined in the object declaration.
• The format for object instantiation is:<object name> = new <class name> (<arguments>);
Object Instantiation
<object name> = new <class name> (<arguments>);
• The <object name> is the name for the object as you declared it in your program.
• The <class name> is the name of the class that this object has been declared to belong to.
• The <arguments> represent possible data that may be required to create the object instance.
Object Instantiation
• We declared three types of Cars:Cars clintsPorsche;
Cars jessicasBMW;
Cars johnsLamborghini;
• To instantiate the instances of the Cars:clintsPorsche = new Car();
jessicasBMW = new Car();
johnsLamborghini = new Car();
Object Instantiation
• The MyFirstApplication instantiates the object mainWindow on line two of the main method:mainWindow = new MainWindow();
MyFirstApplication
MyFirstApplication
Main(String[] args)
mainWindow
MainWindow
Message Passing
• Once an object exists, the program can begin to send messages to the object.– The message must include the object to
which the message is addressed, the name of the task to be completed, and any data required to complete the task.
– In Java speak:
<object name>.<method name>(<arguments>);
Message Passing
• Let’s create messages for our cars.– We need to know how much gas Clint has.
clintsPorsche.getGasLevel();– We want to turn on John’s hazards.
johnsLamborghini.turnOnHazards();
Message Passing
• Now lets look at the last line of the main method in MyFirstApplication.mainWindow.setVisible(true);
Method Declarations
• The method declaration provides the function implementation in the program. – The basic format of a method declaration