In this example the server side codes, handles the input

Static/Class methods

There are two types of methods.

  • Default: Instance methods are associated with an object derived in the caller and use the instance variables of that object to access that method. The method in the serving class, must be declared public to expose to the caller. A private static method will be used by the all the method-members of the serving class.
    • .external_class  EC = new external_class(with_parameter_of_choice);
      • EC.non_static_method(with param-no-pram-choice)--- nonstatic memeber
      • .external_class.Static_member()----static method
  • Static methods can't be called with the the instance variables of any object of the class they are defined in. If you define a method to be static, you will be given a rude message by the compiler if you try to access any instance variables. You can access static variables, but except for constants, this is unusual. Static methods typically take all they data from parameters and compute something from those parameters, with no reference to variables. This is typical of methods which do some kind of generic calculation. A good example of this are the many utility methods in the predefined Math class

 

Create Server:

package server_package;
import java.io.*;
import java.util.*;
//creating a package
// javac serve_static.java
public class serve_static
{
//constructor, will not print since reference is not changed.
public serve_static(){ System.out.println("Package Server Constructor responded");}
//these are the fileds
//and fields properties
public static String str2="";
private static int n1 = 0;
//method starts
//accesor type public
//return type void
//() place holder for parameters
public static void process() throws IOException
{
//Scanner sc = new Scanner(System.in);
//System.out.print("Please type your name :");
InputStreamReader isr = new InputStreamReader(System.in);
BufferedReader br = new BufferedReader(isr);
try
{
System.out.print("Please type name:");
String str = br.readLine();
str2 += str;
System.out.print("Please type age:");
str = br.readLine();
n1 = Integer.parseInt(str);
System.out.print(str2 + " : " + n1);
System.out.println();
}//end try
catch(Exception e) { System.out.println("data was blank");}
}//end of process
}//end of class

Code a client, using the package serve_static, you don't create an object instance to call a static method. The client will call the public-static-method in serve with the declared class name that resides in the server. The same is not true with public-static fields, those can be accessed the instance of the object or direct with the class name.

import java.io.*;
import server_package.*;
// javac client_of_staticmethod.java
public class client_of_staticmethod
{
public static void main(String args[])throws IOException
{

System.out.println("Client Starts");
//creating object where ss is reference to the class simple server_1
serve_static ss = new serve_static(); //will throw an error
serve_static.process();
System.out.println("--Back To Client Print a public member from server ");
System.out.println(serve_static.str2);
System.out.println(ss.str2);
System.out.println("Client ends");

}//end of main
}//end of class