2 Classes, Methods, and Objects
2 Classes, Methods, and Objects
Class Fundamentals
type methodname1(parameter-list){
//body of the method
}
type methodnameN(parameter-list){
//body of the method
}
}
The data/variable within the class is called
instance variable.
Each object has its own copy of variable.
The code is contained within the method.
The method and instance variable is defined
within the class is called member of the
class. that is, things that you access with .
The variable of one object is separate and
unique from the variable of the other object.
class Box {
double width;
double height;
double depth;
}
To create an object of type Box:
Box mybox = new Box();
Now mybox will be an instance of Box.
To access the member of the mybox object
outside the class
mybox.width=100;
// This class declares an object of type Box.
class BoxDemo {
public static void main(String args[]) {
Box mybox = new Box();
double vol;
.......
b1 = null; b1 no longer refers to any specific object. It becomes a reference that doesn't point anywhere.
The original Box object itself might still exist in memory until it's garbage collected, but you can't access it through b1 anymore.
width = 10;
height = 10;
depth = 10;
}
double vol;
double vol;
//finalization code
}
Prevents access to finalize() by code outside of the
class.
Arrays
class AutoArray {
public static void main(String args[]) {
Size can't be writen (we could do this in C)
int month_days[] = { 31, 28, 31, 30, 31, 30, 31, 31,
30, 31, 30, 31 };
System.out.println("April has " + month_days[3] + "
days.");
}
}
Multidimensional Arrays
int twoD[][] = new int[4][5];
This allocates a 4 by 5 array and assigns it to twoD.
In Multidimensional array
you only need to the first dimension (leftmost). (Row)
You can allocate the remaining dimension separately.
System.out.println(strOb1);
System.out.println(strOb2);
System.out.println(strOb3);
}
}
Also Compare => Directly using == [BE AWARE, there is no > / < / >= /..... for string (but != is available)]
if(strOb1.equals(strOb2))
System.out.println("strOb1 == strOb2");
else
Length of strOb1: 12
System.out.println("strOb1 != strOb2"); Index starts from 0
Char at index 3 in strOb1: s
In Method overloading
Two or more method within the same class share
the same name.
But their parameter declarations are different.
Overloaded methods must differ in the type or
the number of parameters.
Methods may have different return type, but it is
not sufficient for method overloading.
// Demonstrate method overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
Output:
No parameters
a: 10
a and b: 10 20
double a: 123.2
Result of ob.test(123.2): 15178.24
Automatic Type Conversion
// Automatic type conversions apply to overloading.
class OverloadDemo {
void test() {
System.out.println("No parameters");
}
Output:
No parameters
a and b: 10 20
Inside Test( double) a :88
Inside Test (double) a: 123.2
Overloading Constructor
/* Here, Box defines three constructors to initialize
the dimensions of a box various ways.*/
class Box {
double width;
double height;
double depth;
double vol;
Test(int i, int j) {
a = i;
b = j;
}
Output:
ob1 == ob2: true
ob1 == ob3: false
Call-by-Reference
A reference to an argument is passed to the parameter.
Used to access the actual argument specified in the call
In Java
This is done by passing an object
Object are passed by reference
Primitive data types are always passed by value.
class Test { Objects, arrays, and strings are passed by reference (a copy of the reference, not the object itself).
Modifications made through the reference variable within the method affect the original object.
int a, b; Java doesn't have direct pointers for primitive data types.
Wrapper classes and modifying objects passed by reference can simulate call by reference
behavior for primitives, but with limitations.
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
Don't get confused; you can pass an object of the same class as where (class) it's written
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
ob.meth(ob);
Output:
When a simple type is passed to
Ob.a and ob.b before call: 15 20 a method, it is done by use of
Ob.a and ob.b after call: 30 10 call-by-value. Objects are passed
by use of call-by-reference.
Returning Objects
// Returning an object.
class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}
}
class RetOb {
Output:
public static void main(String args[]) {
Ob1.a: 2
Test ob1 = new Test(2);
Ob2.a: 12
Test ob2;
Ob2.a after second increase: 22
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "
+ ob2.a);
}
}
class AccessTest {
public static void main(String args[]) {
Test ob = new Test();
class StaticDemo {
static int a = 42;
static int b = 99;
static void callme() {
System.out.println("a = " + a);
}
}
class StaticByName {
public static void main(String args[]) {
StaticDemo.callme();
Output:
System.out.println("b = " + StaticDemo.b);
a = 42
}
b = 99
}
Final Keyword
Just the same as const in C
A variable can declared as final.
Prevents its content from being modified.
Behave like a constant
Lowercase, not uppercase