Limitations of static methods & static block:-
o Only static data members
of a class can be referred in a static block or method.
o A static block or
static method can directly invoke only static methods.
o ‘this’ and ‘super’
keyword cannot be used in a static method or in
a static block.
Program below executes without ‘main’ method
–
class
Test
{
static
{
System.out.println(“It is executing without main…..”);
System.exit(0);
}
}
Not only we can do this much but
also we can do list of things. See the program below : -
- class Test2
- {
- int a;
- public Test2(int x)
- {
- a = x;
- }
- public void display1()
- {
- System.out.println("a = " + a);
- }
- static
- {
- System.out.println("Test is loaded.");
- Test2 t = new Test2(5);
- t.display1();
- P x = new P();
- System.exit(0);
- }
- }
- class P
- {
- public P()
- {
- System.out.println("P is instantiated.");
- }
- static
- {
- System.out.println("P is loaded.");
- }
- }
OUTPUT -
Now, as we saw that we can do
almost everything without having main( ),
then why we need main( ) in our program?
In the
sessions ahead, we will get the answer.
Passing arguments to methods-
In Java,
primitive type arguments are passed to methods by value i.e. their copy is
created in the invoked method.
Objects
are passed by references i.e. in case of objects, copy of their reference
variables is created in the invoked method.
Now the
program below is written to swap the values.
Observe it carefully
to understand how this issue is being resolved.
- public class Swapper
- {
- public static void swap(int x, int y)
- {
- int z;
- z = x;
- x = y;
- y = z;
- }
- }
- public class SwapperTest
- {
- public static void main(String[ ] args)
- {
- int a =5, b = 6;
- System.out.println("a = " + a);
- System.out.println("b = " + b);
- Swapper.swap(a, b);
- System.out.println("After swap");
- System.out.println("a = " + a);
- System.out.println("b = " + b);
- }
- }
The output is -
Our
purpose is not solved. Let’s see the reason for this.
See the
fig2. below
Now,
let’s find out solution to this problem.
- public class MyNumber
- {
- int value;
- public MyNumber(int x)
- {
- value = x;
- }
- }
- public class Swapper
- {
- public static void swap(MyNumber x, MyNumber y)
- {
- int z;
- z = x.value;
- x.value = y.value;
- y.value = z;
- }
- }
- public class SwapperTest
- {
- public static void main(String[ ] args)
- {
- MyNumber a = new MyNumber(5);
- MyNumber b = new MyNumber(6);
- System.out.println("a = " + a.value);
- System.out.println("b = " + b.value);
- Swapper.swap(a, b);
- System.out.println("After swap");
- System.out.println("a = " + a.value);
- System.out.println("b = " + b.value);
- }
- }
Now the problem is fixed as you can see the value is swapped. We handled it by passing value by reference.
Let see the explanation diagrammatically. (Fig. - 3)
Question. Define a class named
Rational that contains 2 data members
to store the value of numerator and denominator of a rational number, default & two parameterized constructors, a display ( ) method that displays the value of a rational object
in
form and add
( ) methods which are referenced by the following class.
Answer –
- package StaticConceptualPrograms;
- public class Rational{int numerator, denominator;public Rational() { }public Rational(int x, int y){numerator = x;denominator = y;}void display(){System.out.println(numerator+"/"+denominator);}Rational add(Rational y){numerator = y.denominator*this.numerator + this.denominator * y.numerator;denominator = y.denominator*this.denominator;return this;}Rational add(Rational x, Rational y){numerator = y.denominator*x.numerator + x.denominator * y.numerator;denominator = y.denominator*x.denominator;return this;}/*static Rational add(Rational x, Rational y){}*/}package StaticConceptualPrograms;public class RationalTest{public static void main(String[] args){Rational a = new Rational(2, 3);Rational b = new Rational(4, 5);System.out.print("Rational a is : ");a.display();System.out.println("");System.out.print("Rational b is : ");b.display();System.out.println("");Rational c = a.add(b);
- System.out.print("Sum of a & b : ");c.display();System.out.println("");Rational r = new Rational(6, 5);Rational s = new Rational(4, 3);Rational t = new Rational();t.add(r, s);System.out.print("Rational r is : ");r.display();System.out.println("");System.out.print("Rational s is : ");s.display();System.out.println("");System.out.print("Rational r & s is : ");t.display();System.out.println("");Rational z;System.out.print("Sum of a, b, r & s is : ");//z = Rational.add(c, t);//z.display();}
- }
Output -
- public class Rational{int p, q;public Rational() {}
- public Rational(int x, int y){p = x;q = y;}public void display(){System.out.println(p+"/"+q);}Rational add(Rational r){Rational s = new Rational();s.p = p * r.q + q * r.p;s.q = q * r.q;return s;}public void add(Rational a, Rational b){p = a.p * b.q + a.q * b.q;q = a.q * b.q;}public static Rational add(Rational a, Rational b){Rational c = new Rational();c.p = a.p * b.q + a.q * b.q;c.q = a.q * b.q;return c;}}public class RationalTest{public static void main(String[] args){Rational a = new Rational(2, 3);Rational b = new Rational(4, 5);System.out.print("Rational a is : ");a.display();System.out.println("");System.out.print("Rational b is : ");b.display();System.out.println("");Rational c = a.add(b);System.out.print("Sum of a & b : ");c.display();System.out.println("");Rational r = new Rational(6, 5);Rational s = new Rational(4, 3);Rational t = new Rational();t.add(r, s);
- System.out.print("Rational r is : ");r.display();System.out.println("");System.out.print("Rational s is : ");s.display();System.out.println("");System.out.print("Rational r & s is : ");t.display();System.out.println("");Rational z;System.out.print("Sum of a, b, r & s is : ");z = Rational.add(c, t);z.display();}}
To understand the logic used inside the different methods of Rational class, please see the diagrams below
Figure
(20.a) to understand public Rational
add(Rational r) method
Program -
- class Test1{int a, b;public Test1(int a, int b){a=a;b=b;}public static void main(String[] args){Test1 t = new Test1(5, 6);t.display();}public void display(){System.out.println("a = " + a);System.out.println("b = " + b);
- }}
Output -