Java Syntax, Java Conventions, CSE 115 Conventions (Part 2) CSE 115 Spring 2006

advertisement
Java Syntax, Java Conventions,
CSE 115 Conventions (Part 2)
CSE 115
Spring 2006
January 30, February 1 & 3, 2006
VII - Constructors
 Q: What is the job of the constructor?
 Identifying the constructor in code:
public class SomeName {
public SomeName() {
Between the { } is the Java code that
outlines what the functionality of the
constructor is.
}
}
VII – Constructors continued
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 NameOfConstructor();
VIII - Keywords
Sometimes called reserved words, these
words have a special meaning in Java and
can only be used for that purpose within
your code.
Please note the listing of keywords for this
semester linked off of the Resources page
IX - Identifiers
 Programmer defined names for program
elements (“names”)
 Rules:
1) Begin with a letter or underscore
2) Followed by zero or more letters,
numbers, underscores
3) No spaces or special characters allowed
in identifiers
4) Keywords are not allowed to be identifiers
XI - Dependency
 Relationship between two classes
 Informally called “uses a”
 Represented in UML diagrams as a dotted arrow
 In code, if ClassA uses a ClassB:
public class ClassA {
public ClassA() {
new ClassB();
}
}
XII - Packages
package keyword indicates the class’
membership in a package.
Packages are ways to organize code so
that code with like purpose is kept
together.
XIII - Comments
Notes to help us remember/understand
the code we write
Two styles:
// to the end of line comment
/* Multi-line comment begin
Multi-line comment ends with */
XIV - Composition
Second relationship between classes
Informally called “has a”
Represented in UML with a diamondheaded arc
In code:
Declare an instance variable
Create an instance of the component part
Assign that instance to the instance variable
XV - Variables
Named storage
XVI – Instance Variables
Variables that store information specific to
a class – used to store all three types of
properties discussed earlier.
Declaring an instance variable:
visibility type identifier;
XVII – Visibility
Access control modifiers indicate who has
access to something
Visibilities are presented by keywords
private
public
Q: What is the difference between the two
types of visibilities?
XVIII – Type of a variable
Java is strongly-typed
When variables are declared, we must tell
Java what type of thing the variable will
hold on to
XIX – Identifiers for instance variables
Begin with an underscore
First letter of first word lower case
First letter of subsequent words upper
case
_myFirstInstanceVariable
Download