define_class_method_field.htm |
Class to object: |
Class is created, with one field and one method or function. Where
function does the work and field keeps data to feed the method or
function. |
|
How to create an object before we can use? with new key word, where pp
is a variable will hold the memory of the object in the computer
memory; pp also bags all the information of the class.
|
In the next move you would like to know, how to use this
method or function. Now, note the key word void, that means we can't expect
any processed data are designed to return to the caller, but it can process
and display with in it's body.
|
|
the code of the above example
import java.io.*;
public class Test_This {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str ="";
Process pp = new Process();//object is created
try{
System.out.print("Please write your name: ");
str = br.readLine();
pp.method_function(str); //calling this method using object pp
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Process
{
//class body
public int n1 =12; //defined and instantiated a filed
public void method_function(String str)
{
//function body
System.out.println("You called me ?: " + str);
System.out.println("Not sure how to get back to you ?: ");
}
}
|
the out put of the above code, meaning the print out was done in an
external class "Named" by a process method_function that set as "void", does
need to return any value to the calling application or routine.
|
Example 2: In this example we will create a function that return some
value to the calling routine. Here we would send a text back to the calling
routine. To do that you have define the method type, here type is String and
scope of the method is public. When you define the method with String data
type, you would note that compiler will complain, if you are using eclipse
you will get this on-focus message.
|
Now to print in the calling routine's space
|
the required codes are given below.
import java.io.*;
public class Test_This {
public static void main(String[] args) throws IOException{
// TODO Auto-generated method stub
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
String str ="";
Process pp = new Process();//object is created
try{
System.out.print("Please write your name: ");
str = br.readLine();
System.out.print(pp.method_function(str)); //calling this method using
object pp
}//try
catch(NumberFormatException e)
{ System.out.println("data was blank");}
}
}
class Process
{
//class body
public int n1 =12; String name = "";//defined and instantiated a filed
public String method_function(String str)
{
//function body
name += str.toUpperCase();
System.out.println("You called me ?: " + str);
System.out.println("Not sure how to get back to you ?: ");
System.out.println("--------------------------");
System.out.println("Now I can get back to you ?: " );
return name;
}
}
|
|
|
|
|