Java Syntax Java Conventions CSE 115 Conventions Part














- Slides: 14

Java Syntax, Java Conventions, CSE 115 Conventions (Part 2) CSE 115 Spring 2006 January 30, February 1 & 3, 2006

VII - Constructors l Q: What is the job of the constructor? l Identifying the constructor in code: public class Some. Name { public Some. Name() { Between the { } is the Java code that outlines what the functionality of the constructor is. } }

VII – Constructors continued l When we actually want to create an object, we need to activate the capability of the constructor, we do so by inserting the following line of code into our program: new Name. Of. Constructor();

VIII - Keywords l Sometimes called reserved words, these words have a special meaning in Java and can only be used for that purpose within your code. l Please note the listing of keywords for this semester linked off of the Resources page

IX - Identifiers l l 1) 2) 3) 4) Programmer defined names for program elements (“names”) Rules: Begin with a letter or underscore Followed by zero or more letters, numbers, underscores No spaces or special characters allowed in identifiers Keywords are not allowed to be identifiers

XI - Dependency Relationship between two classes l Informally called “uses a” l Represented in UML diagrams as a dotted arrow l In code, if Class. A uses a Class. B: l public class Class. A { public Class. A() { new Class. B(); } }

XII - Packages l package keyword indicates the class’ membership in a package. l Packages are ways to organize code so that code with like purpose is kept together.

XIII - Comments l Notes to help us remember/understand the code we write l Two styles: // to the end of line comment /* Multi-line comment begin Multi-line comment ends with */

XIV - Composition l Second relationship between classes l Informally called “has a” l Represented in UML with a diamondheaded arc l In code: ¡ Declare an instance variable ¡ Create an instance of the component part ¡ Assign that instance to the instance variable

XV - Variables l Named storage

XVI – Instance Variables l Variables that store information specific to a class – used to store all three types of properties discussed earlier. l Declaring an instance variable: visibility type identifier;

XVII – Visibility l Access control modifiers indicate who has access to something l Visibilities are presented by keywords ¡ private ¡ public l Q: What is the difference between the two types of visibilities?

XVIII – Type of a variable l Java is strongly-typed l When variables are declared, we must tell Java what type of thing the variable will hold on to

XIX – Identifiers for instance variables l Begin with an underscore l First letter of first word lower case l First letter of subsequent words upper case _my. First. Instance. Variable