This document discusses Java methods, classes, and key concepts like overloading, parameter passing, recursion, access control, static methods/variables, and nested classes. It provides examples of overloading methods based on parameters, passing objects and primitives as parameters, recursively calculating factorials, using access specifiers like public and private, defining static class members, and creating inner classes. It also covers strings, command line arguments, and common classes like String and StringBuffer.
1 of 18
More Related Content
Hemajava
1. N ART ADAR SARASAWATHI COLLEGE OF
ART &SCIENCE,Theni
2. METHODS AND CLASSES
It examies several topics relating to
methods,including overloading,parameter,passing and
recrsion.
The class , discussing access control , the use of the
keyword staic , and or of java’s most important
built-in classes string.
3. Overloading methods
When this is the case the methods are said to be
overloading and the process is referred to as method
overloading.
Thus overloaded methods must may have different
return/or number of their parameters.
two versions of a method :
Overloaded method
Simply executes method
4. As you can see test() is overloaded four times.
at no parameters,
The first version takes no parameters,
The second takes integer parameter,s
Third tat the takes two integer parameters,
Fourth takes one double parameter.
The fact that the fourth of test() also return a value is
of no consequence relative to overloaded.
Since return types do not play a role in overload
resolution.
5. Using objects as parameters
As you can , see the equal() method inside Test
compares two objects for equality and return the
result.
If they contain the same values, that then the method
return true.
Otherwise it return false. Notice that the parameter o
in equals() Test as its type.
Version of Box allows one object to initialize.
6. A closer look at argument passing
There are two way is that a language.
The first way is call-by-value.
Value of argument into the formal parameter of the
subroutine.
The second way an argument can be passed is call-
by-reference.
Not value of the argument.
Operation that occur inside meth() have no effect on
the values of a and b used in the call;
7. Returning objects
A method can return any type of data including class
type that you create.
incrByTen() meyhod.
As you each time incrByTen() is invoked new object is
created and a reference to is returned to the calling
routine.
The all object are dynamically allocated using new, you
don’t need to worry about an object going out-of-
scope.
8. recursion
Java support recursion.
A method that calls itself is said to be recursive.
The classic example recursion product of all the whole
numbers between 1 and N of the factorial of number.
for example 3 factorial 1*2*3, or 6.
Use println() statements liberally during development
.
9. Introducing Access Control
As you know encapsulation link data with the code that
manipulates.
Encapsulation provide another another important
attritube: access control.
Correctly implemented a class create a “black box inner
working not open to tempering.
For example consider the stack class end of true the
methods push() and pop()provide a controlled interface to
the stack not enforced.
Java’s access speccifiers are
public,private,protected.
Protected applies only when inheritance.
10. Let’s begin by defining public and private. when a
member of a class is modified a the public specified
then the member can be accessed by any other code.
Public methods:
setc() and get().
Remove the comment symbol from the beginning of
the.
ob.c=100;
11. Understanding static
There will be times when you will want to define a class
member that will be used independently of any object of
that class.
When a member is decleared static it can be accessed
before any objects of class are created.
Example : static main() decleared as static called before
any objects exist.
Methods declared as static methods:
They can only call other static methods.
They must only access static data.
They cannot refer to this or super in any way.
12. static method from outside its class,
classname.method()
• Inside main(),the static method callme() and
static variable b are accessed through class name
staticDemo.
13. Introducing final
A variable can be declared as final.
Subsequent parts of your program can now use
FILE_OPEN,etc., as if they were constants,without
fear that a value has been changed.
Thus final variable is essentially a constant.
This second usage of final is described in the next
inheritance is described.
14. Array revisited
Now that you know about classes, an important point
can about about arrays:
The size of an array-that is the number of element that
an array can hold-is found in its length instance
variable.
Program create two stacks:
one five elements other eight element deep.
array maintain their own length own information
makes easy create stacks of any size.
15. Inner class named Inner within scope of class Outer.
Inner can directly access the variable outer_x.
The main() method of InnerClassDemo creates
an instance of class outer and invokes its test()
method.
Instance variable of showy().
16. Introducing nested and inner meth
classes
Such classes are known as nested classes.
There are two type of nested classess:
static and non-static.
The most important type of static nested classes inner
class.
The class named outer has one instance variable
named outer_x, one instance method named test()
and defines inner called inner.
Inner class named Inner is defined scope class Outer.
17. Exploring the string class
String is probably the most commonly used in java’s
class library.
• Even string constants are actually string objects.
• If you need to change a string can always create a new
one that contains the modifications.
• Java defines a peer class String called StringBufffer,
• “I like Java”.
18. Using command-line arguments
This is accompolihed passing command-line
arguments to main().
A command –line argument is the information that
directly follow the program’s name on the command
line when is executed.
The first command line argument is stored at args[0]
The second a args[1]