Java M2
Java M2
Introducing Classes
It is the logical construct upon which the entire Java language is built
because it defines the shape and nature of an object.
As such, the class forms the basis for object-oriented programming in Java.
To access these variables, you will use the dot (.) operator.
The dot operator links the name of the object with the name of an instance variable.
For example, to assign the width variable of mybox the value 100, you would use the following statement:
Declaring Objects
As just explained, when you create a class, you are creating a new data type.
Introducing Methods
• Here, type specifies the type of data returned by the method. This
can be any valid type, including class types that you create.
If the method does not return a value, its return type must be void.
The name of the method is specified by name. This can be any legal
identifier other than those already used by other
items within the current scope.
Returning a Value
Constructors
It has the same name as the class in which it resides and is syntactically similar to a
method.
Once defined, the constructor is automatically called when the object is created,
before the new operator completes.
Constructors look a little strange because they have no return type, not even void.
This is because the implicit return type of a class’ constructor is the class type itself.
It is the constructor’s job to initialize the internal state of an object so that the code
creating an instance will have a fully initialized, usable object immediately.
Parameterized Constructors
Parameterized Constructors
Sometimes a method will need to refer to the object that invoked it. To allow
this, Java defines the this keyword.
this can be used inside any method to refer to the current object. That is, this is
always a reference to the object on which the method was invoked.
You can use this anywhere a reference to an object of the current class’ type is
permitted.
Garbage Collection
Since objects are dynamically allocated by using the new operator, you might be wondering how
such objects are destroyed and their memory released for later reallocation.
In some languages, such as traditional C++, dynamically allocated objects must be manually released
by use of a delete operator.
Java takes a different approach; it handles deallocation for you automatically. The technique that
accomplishes this is called garbage collection.
It works like this: when no references to an object exist, that object is assumed to be no longer
needed, and the memory occupied by the object can be reclaimed. There is no need to explicitly
destroy objects.
Overloading Methods
In Java, it is possible to define two or more methods within the same class that share the
same name, as long as their parameter declarations are different.
When this is the case, the methods are said to be overloaded, and the process is referred
to as method overloading.
When an overloaded method is invoked, Java uses the type and/or number of
arguments as its guide to determine which version of the overloaded method to actually
call.
Thus, overloaded methods must differ in the type and/or number of their parameters.
While overloaded methods may have different return types, the return type alone is
insufficient to distinguish two versions of a method.
When Java encounters a call to an overloaded method, it simply executes the version of
the method whose parameters match the arguments used in the call.
call-by-value. call-by-reference.
In general, there are two ways that a computer language can pass an argument to a subroutine.
The first way is call-by-value. This approach copies the value of an argument into the formal
parameter of the subroutine. Therefore, changes made to the parameter of the subroutine have no effect
on the argument.
The second way an argument can be passed is call-by-reference. In this approach, a reference to an
argument (not the value of the argument) is passed to the parameter. Inside
the subroutine, this reference is used to access the actual argument specified in the call. This means that
changes made to the parameter will affect the argument used to call the subroutine.
A method can return any type of data, including class types that you create.
Returning Objects For example, in the following program, the incrByTen( ) method returns an
object in which the value of a is ten greater than it is in the invoking object.
As you know, encapsulation links data with the code that manipulates it.
However, encapsulation provides another important attribute: access control.
Understanding static
Introducing final
Doing so prevents its contents from being modified, making it, essentially, a constant.
This means that you must initialize a final field when it is declared.
The keyword final can also be applied to methods, but its meaning is
substantially different than when it is applied to variables.
It is possible to define a class within another class; such classes are known as
nested classes.
The scope of a nested class is bounded by the scope of its enclosing class. Thus, if class B is
defined within class A, then B does not exist independently of A.
A nested class has access to the members, including private members, of the class in which
it is nested.
However, the enclosing class does not have access to the members of the nested class. A
nested class that is declared directly within its enclosing class scope is a member of its
enclosing class. It is also possible to declare a nested class that is local to a block.
A static nested class is one that has the static modifier applied.
Because it is static, it must access the non-static members of its enclosing class through
an object. That is, it cannot refer to non-static members of its enclosing class directly.
Because of this restriction, static nested classes are seldom used.
An inner class is a non-static nested class. It has access to all of the variables and
methods of its outer class and may refer to them directly in the same way that other non-
static members of the outer class do.