Sie sind auf Seite 1von 10

Assigning Object Reference Variables

1. We can assign value of reference variable to another reference variable.


2. Reference Variable is used to store the address of the variable.
3. Assigning Reference will not create distinct copies of Objects.
4. All reference variables are referring to same Object.

We can assign one object reference to another object as

Box b1 = new Box();


Box b2 = b1;

class Rectangle {
int length;
int breadth;
}
class RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
Rectangle r2 = r1;
r1.length = 10;
r2.length = 20;
System.out.println("Value of R1's Length : " + r1.length);
System.out.println("Value of R2's Length : " + r2.length);

}
}

Methods in Java Classes


1. return_type is nothing but the value to be returned to an calling method.
2. method_name is an name of method that we are going to call through any method.
3. arg1,arg2,arg3 are the different parameters that we are going to pass to a method.

class Rectangle {
int length;
int breadth;

void setLength(int len)


{
length = len;
}
}
class RectangleDemo {
public static void main(String args[]) {

Rectangle r1 = new Rectangle();

r1.length = 10;
System.out.println("Before Function Length : " + r1.length);

r1.setLength(20);
1
System.out.println("After Function Length : " + r1.length);

}
}

Returning Value from the Method


1. We can specify return type of the method as “Primitive Data Type” or “Class name”.
2. Return Type can be “Void” means it does not return any value.
3. Method can return a value by using “return” keyword.

class Rectangle {
int length;
int breadth;
void setLength(int len)
{
length = len;
}
int getLength()
{
return length;
}
}

class RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
r1.setLength(20);
int len = r1.getLength();
System.out.println("Length of Rectangle : " + len);

}
}

this keyword : Refer Current Object in Java Programming


1. this is keyword in Java.
2. We can use this keyword in any method or constructor.
3. this keyword used to refer current object.
4. Use this keyword from any method or constructor to refer to the current object that calls
a method or invokes constructor
class Rectangle {
int length;
int breadth;

void setDiamentions(int ln,int br)


{
this.length = ln;
this.breadth = br;
}

2
class RectangleDemo {
public static void main(String args[]) {

Rectangle r1 = new Rectangle();

r1.setDiamentions(20,10);

System.out.println("Length of Rectangle : " + r1.length);


System.out.println("Breadth of Rectangle : " + r1.breadth);

}
}

Constructors

1. Constructor name is same as that of “Class Name“.


2. Constructor is used to Initializes an Object.
3. Constructor cannot be called like methods.
4. Constructors are called automatically as soon as object gets created.
5. Constructor don’t have any return Type. (even Void)
6. Constructor can accept parameter.

class Rectangle {
int length;
int breadth;

Rectangle()
{
length = 20;
breadth = 10;
}

class RectangleDemo {
public static void main(String args[]) {

Rectangle r1 = new Rectangle();

System.out.println("Length of Rectangle : " + r1.length);


System.out.println("Breadth of Rectangle : " + r1.breadth);

}
}

class Rectangle {
int length;
int breadth;
Rectangle()
{
length = 20;
breadth = 10;
3
}
void setDiamentions()
{
length = 40;
breadth = 20;
}
}

class RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle();
System.out.println("Length of Rectangle : " + r1.length);
System.out.println("Breadth of Rectangle : " + r1.breadth);
r1.setDiamentions();
System.out.println("Length of Rectangle : " + r1.length);
System.out.println("Breadth of Rectangle : " + r1.breadth);
}
}

Parameterized Constructors :
1. Constructor Can Take Value , Value is Called as – “Argument“.
2. Argument can be of any type i.e Integer,Character,Array or any Object.
3. Constructor can take any number of Argument.
class Rectangle {
int length;
int breadth;
Rectangle(int len,int bre)
{
length = len;
breadth = bre;
}
}
class RectangleDemo {
public static void main(String args[]) {

Rectangle r1 = new Rectangle(20,10);

System.out.println("Length of Rectangle : " + r1.length);


System.out.println("Breadth of Rectangle : " + r1.breadth);

}
}

Garbage Collection : Destroying Object in Java Programming


1. Object is Created Using new Operator.
2. Object gets memory inside “Heap“.
3. In C++ After Using Object , Memory for Object gets de-allocated using delete().
4. In Java De-allocation of Object Can be done automatically.
5. Automatic Technique used in Java Programming Which is used to de-allocate memory is
called as “Garbage Collection“.
6. Java is Smart Enough to identify the Unused objects or Useless Objects.

4
overloading
class poly{
void add(int a, int b)
{
System.out.println("sum of two" +(a+b));
}
void add(int a, int b, int c)
{
System.out.println("sum of three" +(a+b+c));
}
}
public class Overload {
public static void main(String args[]) {
poly p=new poly();
p.add(10,55);
p.add(10,6,9);

}
}

Over riding
class one{
void calculate(int x)
{
System.out.println("square of two" +(x*x));
}
}

class two extends one{


void calculate(int x)
{
System.out.println("cube" +(x*x*x));
}
}

public class Override {


public static void main(String args[]) {

one o=new one();


o.calculate(5);
two t=new two();
t.calculate(5);

}
}

5
STACK IMPLEMENTATION
In general stack class is used to support object oriented principle encapsulation. Satck is having
operations like push() and pop() to insert and delete elements respectively.

Stack follows the principle Last In First Out(LIFO)


class Stack {
int stck[] = new int[10];
int tos;

// Initialize top-of-stack
Stack() {
tos = -1;
}
// Push an item onto the stack
void push(int item) {
if(tos==9)
System.out.println("Stack is full.");
6
else
stck[++tos] = item;
}
// Pop an item from the stack
int pop() {
if(tos < 0) {
System.out.println("Stack underflow.");
return 0;
}
else
return stck[tos--];
}
}
class TestStack {
public static void main(String args[]) {
Stack mystack1 = new Stack();
Stack mystack2 = new Stack();
// push some numbers onto the stack
for(int i=0; i<10; i++)
mystack1.push(i);
for(int i=10; i<20; i++)
mystack2.push(i);
// pop those numbers off the stack

System.out.println("Stack in mystack1:");
for(int i=0; i<10; i++)
System.out.println(mystack1.pop());
System.out.println("Stack in mystack2:");
for(int i=0; i<10; i++)
System.out.println(mystack2.pop());
}
}
finalyze()
finalyze() method is called before Garbage collector reclaim the object
its last chance for any object to perform cleanup activity that is releasing any system resources
held,closing connection if open etc
finalize() is defined in java.lang.Object class
its the responsibility of developer to call superclass finalize method when we override finalyze()
there is no guarantee that finalize() is called because finalyze()
is called just before the garbage collection process happens and we are not sure when garbage
collection process happens

passing and returning object as parameter(passing by reference)

When we pass a primitive type to a method, it is passed by value. But when we pass an object
to a method, the situation changes dramatically, because objects are passed by what is
effectively call-by-reference. Java does this interesting thing that’s sort of a hybrid between
pass-by-value and pass-by-reference

class Rectangle {
int length;
int width;

7
Rectangle(int l, int b) {
length = l;
width = b;
}

void area(Rectangle r1) {


int areaOfRectangle = r1.length * r1.width;
System.out.println("Area of Rectangle : " + areaOfRectangle);
}
}
lass RectangleDemo {
public static void main(String args[]) {
Rectangle r1 = new Rectangle(10, 20);
r1.area(r1);
}
}

Parameter passing techniques


1. Call by value
2. Call by reference

static:

When a member is declared static, it can be accessed before any objects of its class are created,
and without reference to any object.
Methods declared as static have several restrictions:

8
• They can only directly call other static methods.
• They can only directly access static data.
• They cannot refer to this or super in any way

class UseStatic {
static int a = 3;
static int b;
static void meth(int x)
{
System.out.println("x = " + x);
System.out.println("a = " + a);
System.out.println("b = " + b);
}
static {
System.out.println("Static block initialized.");
b = a * 4;
}
public static void main(String args[]) {
meth(42);
}
}
Output:
Static block initialized.
x = 42 a=3 b = 12
As soon as the UseStatic class is loaded, all of the static statements are run.
· First, a is set to 3, then the static block executes, which prints a message and then
initializes b to a*4 or 12. Then main( ) is called, which calls meth( ), passing 42 to x.
· The three println( ) statements refer to the two static variables a and b, as well as to the
local variable x

Introducing Nested and Inner Classes:

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.

// Demonstrate an inner class.


class Outer {
int outer_x = 100;
9
void test() {
Inner inner = new Inner();
inner.display();
}
// this is an inner class
class Inner {
void display() {
System.out.println("display: outer_x = " + outer_x);
}
}
}
class InnerClassDemo {
public static void main(String args[]) {
Outer outer = new Outer();
outer.test();
}
}
Output : display: outer_x = 100

10

Das könnte Ihnen auch gefallen