In this section we will learn how to use static method and how can display multiplication of two any number. The static method use no instance variables of any object of the class they are defined in example.
Squaring of two number in Static class
In this section we will learn how to use static method and how can display multiplication of two any number. The static method use no instance variables of any object of the class they are defined in example.
Description of program
Here this program calculate the any two number
. First of all, we have to define class named "StaticSquare" .
To square a static method are define main class and passing the two
parameter of integer type values. Then we have to define the two integer type
values in the class method. We are going to create object for calling the two
number by using this method "staticSquare.main(a,b)" and after
that "staticSquare.square1()" is return the integer type
values for x and "staticSquare.square2()" is return the integer
type values for y value. Now we are going to used parseInt method for
converting the argument value into the string values. So output will be display
on the screen by using the System.out.println()
method in the command prompt.
Here is the code of this program
import java.io.*; class StaticSquare { public static int x; public static int y; public static void main(int x1, int y1){ x=x1; y=y1; } public int square1(){ return (x*x); } public int square2(){ return (y*y); } public static void main(String args[]){ try{ StaticSquare staticSquare=new StaticSquare(); BufferedReader sq=new BufferedReader (new InputStreamReader(System.in)); System.out.println("Enter two number"); int a=Integer.parseInt(sq.readLine()); int b=Integer.parseInt(sq.readLine()); staticSquare.main(a,b); System.out.println("value1 is=" + a); System.out.println("value2 is="+ b); int sq1=staticSquare.square1(); int sq2=staticSquare.square2(); System.out.println("square of value1 is= " + sq1); System.out.println("square of value2 is= "+ sq2); } catch(Exception e){} } } |