Declarations and Access Control

Java Refresher

Class A template that describes the kinds of state and behavior

that objects of its type support.

Object At runtime, when the Java Virtual Machine (JVM) encounters the new keyword, it will use the appropriate class to make an object which is an instance of that class. That object will have its own state, and access to all of the behaviors defined by its class.

State (instance variables) Each object (instance of a class) will have its own unique set of instance variables as defined in the class. Collectively, the values assigned to an object’s instance variables make up the object’s state.

Behavior (methods) When a programmer creates a class, she creates methods  for that class. Methods are where the class’ logic is stored. Methods are where the real work gets done. They are where algorithms get executed, and data gets manipulated.

Identifiers and Keywords

Java Refresher

Identifiers

All the Java components we just talked about—classes, variables, and methods need names. In Java these names are called identifiers.

Example:-  Animal , employeeName, setName(String name)

Keywords

Like all programming languages, Java has a set of built-in keywords. These keywords must not be used as identifiers.

Example :- int , long, if ,else, while, float, double, Integer,Double

Inheritance

Java Refresher

Inheritance

Central to Java and other object-oriented languages is the concept of inheritance, which allows code defined in one class to be reused in other classes.

Example :- a Car superclass class could define general methods common to all automobiles, but a Ferrari subclass could override the accelerate() method.

Interfaces

Java Refresher

Interfaces

A powerful companion to inheritance is the use of interfaces. Interfaces are like a 100-percent abstract superclass that defines the methods a subclass must support, but not how they must be supported.

Example:-

interface Animal

{

 void eat();

}

class Cow implements Animal

{

void eat()

{

System.out.println(“Vegitarian”);

}

Identifiers & JavaBeans

Legal Identifiers

1)     Identifiers must start with a letter, a currency character ($), or a connecting character such as the underscore ( _ ). Identifiers cannot start with a number!

2)     After the first character, identifiers can contain any combination of letters,currency characters, connecting characters, or numbers.

3)     In practice, there is no limit to the number of characters an identifier can contain.

4)    You can’t use a Java keyword as an identifier.

5)    Identifiers in Java are case-sensitive; foo and FOO are two different identifiers.

Legal Identifiers

Examples of legal and illegal identifiers follow, first some legal identifiers:

int _a;

        int $c;

        int ______2_w;

        int _$;

        int this_is_a_very_detailed_name_for_an_identifier;

     The following are illegal (it’s your job to recognize why):

      int :b;

      int -d;

      int e#;

      int .f;

      int 7g;

Complete List of Java Keywords

http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html

Sun’s Java Code Conventions

Classes and interfaces

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 uppercase (a format that’s sometimes called “CamelCase“). For classes, the names should typically be nouns. For example:

Dog

Account

PrintWriter

       For interfaces, the names should typically be adjectives like

          Runnable

Serializable

Methods

The first letter should be lowercase, and then normal camelCase rules should be used. In addition, the names should typically be verb-noun pairs. For example:

         getBalance

doCalculation

setCustomerName

Variables

        Like methods, the camelCase format should be used, starting with a lowercase letter. Sun recommends short, meaningful names, which sounds good to us. Some examples: buttonWidth;accountBalance;

Constants

        Java constants are created by marking variables static and

       final. They should be named using uppercase letters with underscore characters as separators:

            MIN_HEIGHT

for more download ppt:  Java_PPT

Advertisements