Welcome to Scribd, the world's digital library. Read, publish, and share books and documents. See more
Download
Standard view
Full view
of .
Look up keyword
Like this
2Activity
0 of .
Results for:
No results containing your search query
P. 1
JPR 01

JPR 01

Ratings: (0)|Views: 4 |Likes:
Published by api-3728136

More info:

Published by: api-3728136 on Nov 29, 2009
Copyright:Attribution Non-commercial

Availability:

Read on Scribd mobile: iPhone, iPad and Android.
download as PDF, TXT or read online from Scribd
See more
See less

03/18/2014

pdf

text

original

2.10 Static Members

static int cal;
static float min = 1;
static void display(int x)

\u2022
They can only call other static methods.
\u2022
They must only access static data.
\u2022
They cannot refer tothi s orsuper in any way. (The keyword super relates to
inheritance and is described in the next topic).
//Demonstration of static members
class MyWork

{
static int x = 10;
static int count = 1;
static void display()
{

System.out.println("Static has initialized...");
}static void increment()

{
System.out.println("Function call : "+count);
count++;

}
}class StaticMember

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

MyWork.display();

//statement1
System.out.print("Value of x: ");
System.out.println(MyWork.x);

//statement2
MyWork.increment();
//statement3
MyWork.increment();
//statement4
MyWork.increment();
//statement5
}
}
Program 2.9 Program using static variables and methods

// Call by value.
class Test
{

void meth(int i, int j)

{
i++;
j++;

}
}class CallByValue

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

Test ob = new Test();
int a = 22, b = 93;
System.out.print("a and b before call: ");

System.out.println(a + " " + b);
ob.meth(a, b);
System.out.print("a and b after call: ");

System.out.println(a + " " + b);
}
}
Program 2.10 Example of Call by Value

// Objects are passed by reference.
class Test
{

int a, b;
void meth(Test o)

{
o.a++;
o.b++;

}
}class CallByRef

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

Test ob = new Test();

ob.a = 22;
ob.b = 93;
System.out.println("ob.a and ob.b before call: " +

ob.a + " " + ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +

ob.a + " " + ob.b);
}
}
Program 2.11 Example of Call by Reference
//A method returning object
class Square

{
int n;
Square(int x)
{

n = x;
}Square change()

{
Square temp = new Square(n);
temp.n = temp.n * temp.n;
return(temp);

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

{
Square s = new Square(8);
Square t;
t = s.change();
System.out.println("Square of 8 is: "+t.n);

}
}
Program 2.12 Method which returns object

//Finding factorial using recursion
class Factorial
{

int fact(int n)
{
if(n==1)
return 1;
//statement1
elsereturn(n*fact(n-1));
}public static void main(String args[])

{
Factorial f = new Factorial();
System.out.print("Factorial of 4:");
System.out.println(f.fact(4));
System.out.print("Factorial of 5:");
System.out.println(f.fact(5));
System.out.print("Factorial of 6:");
System.out.println(f.fact(6));

}
}
Program 2.13 Calculating factorial using recursion
Nested and inner classes
// Demonstration of an inner class.
class Outer
//Outer class

{
int out_x;
Outer(int x)
{

out_x = x;
}void test()

{
Inner inner = new Inner();
inner.display();

}class Inner
//Inner class

{
void display()
{

You're Reading a Free Preview

Download
scribd
/*********** DO NOT ALTER ANYTHING BELOW THIS LINE ! ************/ var s_code=s.t();if(s_code)document.write(s_code)//-->