Monday, April 19, 2010

WORKING WITH THE JAVA CLASS

[] Object-Oriented programming or OOP

– Revolves around the concept of objects as the basic elements of your programs.
– These objects are characterized by their properties and behaviors.



[] Encapsulation


– the method of hiding certain elements of the implementation of a certain class.
– By placing a boundary around the properties and methods of our objects, we can prevent our programs from having side effects wherein programs have their variables changed in unexpected ways.


[] Class and Objects

[] Class

– can be thought of as a template, a prototype or a blueprint of an object
– is the fundamental structure in object-oriented programming

Two types of class members:

– Fields (properties or attributes)

● specify the data types defined by the class

– Methods.

● specify the operations



[]Object

– is composed of a set of data (properties) which are variables describing the essential characteristics of the object, and consists of a set of methods (behavior) that describes how an object behaves.
– An object is an instance of a class.



[]Class Variables

Classes consist of

– Instance variables
– Instance methods
– Class Variables (static member variables)
● variables that belong to the whole class.
● This means that they have the same value for all the objects in the same class.



[]Class Instantiation

● To create an object or an instance of a class, we use the new operator.
● The new operator
– allocates a memory for that object and returns a reference of that memory location to you.
– When you create an object, you actually invoke the class'constructor.
● The constructor
– is a method where you place all the initializations, it has the same name as the class.



Methods

– is a separate piece of code that can be called by a main program or any other method to perform some specific function.

● The following are characteristics of methods:
– It can return one or no values
– It may accept as many parameters it needs or no parameter at all. Parameters are also called arguments.
– After the method has finished execution, it goes back to the method that called it.



[] Method Declaration


- Return the character at the specified index. An index ranges from 0 to lenght()-1. The first character of the sequence is at index 0, the next at index1, and so on, as for array indexing.

[] Static Methods

– methods that can be invoked without instantiating a class (means without invoking the new keyword).
– Static methods belong to the class as a whole and not to a certain instance (or object) of a class.
– Static methods are distinguished from instance methods in a class definition by the keyword static.

● To call a static method, just type,Classname.staticMethodName(params);



Parameter Passing

[] Pass-by-Value

– when a pass-by-value occurs, the method makes a copy of the value of the variable passed to the method. The method cannot accidentally modify the original argument even if it modifies the parameters during calculations.

– all primitive data types when passed to a method are pass-by-value.


[] Pass-by-Reference
– When a pass-by-reference occurs, the reference to an object is passed to the calling method. This means that, the method makes a copy of the reference of the variable passed to the method.

– However, unlike in pass-by-value, the method can modify the actual object that the reference is pointing to, since, although different references are used in the methods, the location of the data they are pointing to is the same.

No comments:

Post a Comment