Hello...
It's nice to have you visiting my posting. Let's get start to the points and main objectives, SCJP. By this time we have to assume you have basic understanding of Java. If not, please keep the pace and hope you are willing to take longer time and patience.
In this first chapter we'll talk about Declacations and Access Control which is divided into 4 sub-chapters
# Identifiers and JavaBeans
# Class Declaration
# Interface Declaration
# Class Members Declaration
Before we get deeper to each of them, lets review some basic understandings and terms in Java.
a) Class : it's not where you have your lecture, but class in Java is a template with state and behavior that an object has.
b) Object : as the Java Virtual Machine (JVM) runs at runtime, it will create a new object from class when it encounters "new" key word.
c) State (instance variables) : values assigned to objects' instance are the object's state
d) Behavior (methods) : methods is the behavior or actions of the class. Methods are the actual excecutor of actions in the
Identifiers and Keywords
Identifiers are the names for Java components, like class' name and object's name. Later we'll talk more detail about the legal rules for identifiers.
Just like the other programming language, Java also has several reserverd words known as keywords which cant be used as identifiers.
Inheritance
As an Object-Oriented Programming (OOP), Java has the concept of inheritance which means that codes defined in one class can be used in other classes, known as parent-child relation. The subclass has every Class members that superclass possesses that are assigned public or protected.
For instance, an "Animal" superclass has numOfLegs and run(), then its subclass, "Horse" will also have both of them, and it can aslo override the "run()"
Interfaces
One superb example of 'inheritance' is interface which is like a perfect abstract superclass because it only defines the methods on the superclass but it doesn't support the way it should be implemented. The subclass, therefore, should implement those superclass methods.
Finding Other Classes
It's impossible to create a program totally from scratch, whether you want or not, you will need to use another programmer's code such as Java's API classes or your friends' special function code. By organizing the classes into "package", you can use "import" to get access to that out-source code.
Showing posts with label Declarations and Access Control. Show all posts
Showing posts with label Declarations and Access Control. Show all posts
Monday, February 15, 2010
Sun's Java Code Convention
To provide good maintenance for Java Programming, Sun has a coding convention so that all programmers will have same code-typing habit. Here are some conventions agreed by Java developers
a) Classes and interfaces : should be in camelCase, the first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be capitalized as well.
For classes, the name is typically noun, such as
Horse
DiskStorage
UserName
For interfaces, the name is typically adjectives, such as
Clickable
Runnable
b) Methods : the first letter should be lowercase, in camelCase form, and verb-noun combination, for example
getHeight
kickHard
setUserName
c) Variable: first character should be lowercase, then next characters are in camelCase, and it should be short yet meaningful, such as:
myAccount
boxHeight
d) Constant: in Java, you can use "static" and "final" keyword to creat constant. By convention, constant should be all in UPPERCASE wih underscore for sparation between characters, such as:
BOX_WIDTH
ACCOUNT_NUMBER
Java Beans Property
There are also some convention for JavaBean properties
a) if property is a boolean, the getter method is either "is" or "get" ("is" is preferable) like
isRound()
isStarted()
b) if the property is not boolean, the getter should be "get", such as
getHeight()
getTotalAccount()
c) the setter for both boolean and not boolean should be "set". Like "setMaxHeight" is the correct way to set the value or maxHeight variable.
d) The first character after is, set, or get should be in capital.
e) Setter must posses public signatures and void return type
f) Getter must be public with no argument and matched return type with setter method
There are also some rules for JavaBean Listener Naming mechanism
a) Listeners are used to register a listener and must use prefix "add" followed by listener type. For example:
addMouseClickListener() is a valid method to event source to register.
b) Listeners used to remove event sources, should use "remove" prefix, such as
removeMouseClickListener()
c) the type of added/moved listener must add as arguments. For instance:
public void addMyListener(MyListener m)
a) Classes and interfaces : should be in camelCase, the first letter should be capitalized, and if several words are linked together to form the name, the first letter of the inner words should be capitalized as well.
For classes, the name is typically noun, such as
Horse
DiskStorage
UserName
For interfaces, the name is typically adjectives, such as
Clickable
Runnable
b) Methods : the first letter should be lowercase, in camelCase form, and verb-noun combination, for example
getHeight
kickHard
setUserName
c) Variable: first character should be lowercase, then next characters are in camelCase, and it should be short yet meaningful, such as:
myAccount
boxHeight
d) Constant: in Java, you can use "static" and "final" keyword to creat constant. By convention, constant should be all in UPPERCASE wih underscore for sparation between characters, such as:
BOX_WIDTH
ACCOUNT_NUMBER
Java Beans Property
There are also some convention for JavaBean properties
a) if property is a boolean, the getter method is either "is" or "get" ("is" is preferable) like
isRound()
isStarted()
b) if the property is not boolean, the getter should be "get", such as
getHeight()
getTotalAccount()
c) the setter for both boolean and not boolean should be "set". Like "setMaxHeight" is the correct way to set the value or maxHeight variable.
d) The first character after is, set, or get should be in capital.
e) Setter must posses public signatures and void return type
f) Getter must be public with no argument and matched return type with setter method
There are also some rules for JavaBean Listener Naming mechanism
a) Listeners are used to register a listener and must use prefix "add" followed by listener type. For example:
addMouseClickListener() is a valid method to event source to register.
b) Listeners used to remove event sources, should use "remove" prefix, such as
removeMouseClickListener()
c) the type of added/moved listener must add as arguments. For instance:
public void addMyListener(MyListener m)
Labels:
Declarations and Access Control
Identifiers & JavaBeans
Welcome back my friend...as you visit this posting, we can assume that you've had quite seriousness in preparing for SCJP. Let's keep up our pace of working for SCJP so that high score is a very feasible dream.
Let's start with Java identifiers convention
a) Legal identifiers means that your code is syntatically legal.
b) Sun's Java Code Convention: It's the recommended naming mechanism by Sun Microsystem for more standardized naming system for all Java programmers
c) JavaBeans Naming Standards is not listed on the exam but you will need them in real-life programming.
Legal Identifiers
To have legal names in Java programming, you need to know some rules for creating legal identifiers
a) identifier must start with a letter, a currency sign, and connecting character (such as underscore). Identifiers must not start with a number.
b) the second character then can contain any combination of letters, currency characters, connecting characters, or numbers
c) there's no limit for characters' length
d) no keyword should be used (click here for Java keyword)
e) in Java, identifiers are case-sensitive; "animal" dan "Animal" are 2 different identifiers in Java.
examples of legal names are:
int $currency12;
long ___4_;
short i_love_this_crazy_long_identifier_name;
examples for illegal names are:
int -wrong-number;
short 9milk;
double sharp#;
Let's start with Java identifiers convention
a) Legal identifiers means that your code is syntatically legal.
b) Sun's Java Code Convention: It's the recommended naming mechanism by Sun Microsystem for more standardized naming system for all Java programmers
c) JavaBeans Naming Standards is not listed on the exam but you will need them in real-life programming.
Legal Identifiers
To have legal names in Java programming, you need to know some rules for creating legal identifiers
a) identifier must start with a letter, a currency sign, and connecting character (such as underscore). Identifiers must not start with a number.
b) the second character then can contain any combination of letters, currency characters, connecting characters, or numbers
c) there's no limit for characters' length
d) no keyword should be used (click here for Java keyword)
e) in Java, identifiers are case-sensitive; "animal" dan "Animal" are 2 different identifiers in Java.
examples of legal names are:
int $currency12;
long ___4_;
short i_love_this_crazy_long_identifier_name;
examples for illegal names are:
int -wrong-number;
short 9milk;
double sharp#;
Labels:
Declarations and Access Control
Java Keywords
As in all programming language, in Java, Sun has keywords, and here are all of them
abstract boolean break byte case catch char class const continue default do double else extends final finally float for goto if implements import instanceof int interface long native new package private protected public return short static striptfp super switch synchronized this throw throws transient try void volative while assert (added in Java 1.4) enum (added in Java 1.5)
Labels:
Declarations and Access Control