Results 1 to 4 of 4
  1. #1
    a45b22chp is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default Inner Class and Calling Values

    I have 2 java files, each with their main method. I want to keep each as "public static void main(String argv[]) throws Exception
    { ....."


    I want class A to instantiate a value, and return it so class B can access it. So I think what I need to do is create an inner class within class A that returns a value (double) for B to use - but I don't know how to go about this.


    thanks in advance

  2. #2
    Zosden's Avatar
    Zosden is offline Senior Member
    Join Date
    Apr 2008
    Posts
    384
    Rep Power
    7

    Default

    please show some code. You can't call two main methods you call one or the other.
    My IP address is 127.0.0.1

  3. #3
    a45b22chp is offline Member
    Join Date
    Apr 2008
    Posts
    5
    Rep Power
    0

    Default

    Client asks user to input a String, parses data and sends to Cache, which forwards it to server. Server parses data, sends to Cache, which forwards to Client, which then displays code.

    I need to clock different aspects of the above, and so I define start1 and finish in Client. Start2 = time after Server delay immediately before sending data to Cache.

    Approx time with delay = finish-start1.
    Approx time without delay = finish-start2.

    I figure I need to access start2 from Client.

    Each is their own java file. I right click wach one in Eclipse and "Run as Java Application". Must be run in the order of Server, Cache, Client.


    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.Date;
    
    
    class TCPServer {
    public static double main(String argv[]) throws Exception
    {
    	String clientSentence;
    	String capitalizedSentence;
    	ServerSocket welcomeSocket = new ServerSocket
    	(6503);
    	while (true){
    		Socket connectionSocket = welcomeSocket.accept();
    		BufferedReader inFromClient = 
    			new BufferedReader (new InputStreamReader(
    					connectionSocket.getInputStream()));
    		
    		
    		double rand = Math.random();
    		double result = ((10)* rand); // Max delay is 10 seconds, Min is 0
    		
    		System.out.println("Your current delay is " + result  + " seconds." +'\n');
    		
    		Date now, done;
    		
    		now = new Date();
    			done = new Date();
    			
    		
    		while (now.getTime()+(result * 1000)> done.getTime()) {done = new Date();}
    		
    		/*class server {
    			public double start() throws Exception
    			{
    				
    			//class server = TCPServer.getClass();
    		
    			double start2 = System.currentTimeMillis();
    			return start2;
    		}}
    			*/
    		
    		double start2 = System.currentTimeMillis();
    		
    		
    		
    		DataOutputStream outToClient = 
    			new DataOutputStream(
    					connectionSocket.getOutputStream());
    				clientSentence = inFromClient.readLine();
    		capitalizedSentence = 
    			clientSentence.toUpperCase();
    		
    		
    		
    		outToClient.writeBytes(capitalizedSentence + '\n');
    		return start2;
    		
    	}
    	
    }
    	
    
    }
    Java Code:
    import java.io.*;
    import java.net.*;
    
    
    class TCPCache {
    public static void main(String argv[]) throws Exception
    {
    	String clientSentence;
    	//String capitalizedSentence;
    	ServerSocket welcomeSocket = new ServerSocket(6603);
    	while (true){
    
    		Socket connectionSocket = welcomeSocket.accept();
    		BufferedReader inFromClient = new BufferedReader (new InputStreamReader(connectionSocket.getInputStream()));
    		DataOutputStream outToClient = new DataOutputStream(connectionSocket.getOutputStream());
    		clientSentence = inFromClient.readLine();
    		
    		// capitalizedSentence = 
    		//clientSentence.toUpperCase() + '\n';
    		
    		//capitalizedSentence = CacheClient(clientSentence);
    		//capitalizedSentence = clientSentence;
    	//	outToClient.writeBytes(capitalizedSentence + '\n');
    		
    	
    	//public static String CacheClient(String sentence) throws Exception
    	 
    	
    		String sentence;
    		
    		String modifiedSentence;
    		//BufferedReader inFromUser = new BufferedReader (
    		//new InputStreamReader (System.in));
    
    		Socket clientSocket2 = new Socket("localhost", 6503);
    		DataOutputStream outToServer = new DataOutputStream(clientSocket2.getOutputStream());
    		BufferedReader inFromServer = new BufferedReader (new InputStreamReader(clientSocket2.getInputStream()));
    		sentence = clientSentence;
    		//modifiedSentence = inFromServer.readLine();
    		
    		outToServer.writeBytes(sentence + '\n');
    		modifiedSentence = inFromServer.readLine();
    
    		outToClient.writeBytes(modifiedSentence + '\n');
    		clientSocket2.close();
    		//return modifiedSentence;
    				
    	
    	
    
    	}
    }}

    Java Code:
    import java.io.*;
    import java.net.*;
    import java.util.*;
    
    
    
    
    class TCPClient {
    public static void main(String argv[]) throws Exception
    {
    	String sentence;
    	String modifiedSentence;
    System.out.println( " Hi! Client awaiting input: "+  '\n');
    	
    	BufferedReader inFromUser = new BufferedReader(new InputStreamReader (System.in));
    	Socket clientSocket = new Socket("localhost", 6603);
    
    	DataOutputStream outToServer = new DataOutputStream(clientSocket.getOutputStream());
    	BufferedReader inFromServer = new BufferedReader (new InputStreamReader(clientSocket.getInputStream()));
    	sentence = inFromUser.readLine();
    	double start1 = System.currentTimeMillis();
    	
    	outToServer.writeBytes(sentence + '\n');
    	
    	modifiedSentence = inFromServer.readLine();
    	double finish = System.currentTimeMillis();
    	System.out.println("FROM SERVER: " + modifiedSentence + '\n');
    	
    	TCPServer a = new TCPServer();
    	
    	//class server = TCPServer.getClass;
    	//double start2 =  a.main();
    	
    	
    		//class.TCPServer finish;
    	//System.out.println(ouch);
    	//ublic void invokeOneWay (double[] finish);
    	
    	System.out.println("RTT with delay is approx " + ((finish - start1) /1000) + " seconds" + '\n');
    	System.out.println("RTT without delay is approx " + ((finish - start2)/1000) + " seconds" + '\n');
    	
    	
    	
    	clientSocket.close();
    	
    }
    }

  4. #4
    danielstoner's Avatar
    danielstoner is offline Senior Member
    Join Date
    Apr 2008
    Location
    Canada
    Posts
    191
    Rep Power
    7

    Default

    If you run each class then you end up with 3 processes. In this case you cannot access the objects from one JVM process from another JVM process unless you use something like RMI or another IPC method. So the inner class is not going to help you here.

    If you want to learn about inner classes and how they can be used here are some links to my tutorials on the subject:
    Static Nested Classes | Little Tutorials
    Static Nested Interfaces | Little Tutorials
    Inner Classes | Little Tutorials
    Local Inner Classes | Little Tutorials
    Anonymous Inner Classes | Little Tutorials
    Daniel @ [www.littletutorials.com]
    Language is froth on the surface of thought

Similar Threads

  1. Calling a method in another class
    By uncopywritable in forum New To Java
    Replies: 9
    Last Post: 10-22-2012, 05:01 PM
  2. Replies: 4
    Last Post: 04-15-2008, 02:04 PM
  3. Calling method from another class
    By asahli in forum New To Java
    Replies: 1
    Last Post: 12-15-2007, 07:24 PM
  4. Problem calling another class
    By adlb1300 in forum New To Java
    Replies: 3
    Last Post: 10-25-2007, 03:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •