Results 1 to 4 of 4
Thread: Inner Class and Calling Values
- 04-25-2008, 06:35 PM #1
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
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
- 04-25-2008, 06:39 PM #2
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
- 04-25-2008, 06:46 PM #3
Member
- Join Date
- Apr 2008
- Posts
- 5
- Rep Power
- 0
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(); } }
- 04-25-2008, 08:48 PM #4
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 TutorialsDaniel @ [www.littletutorials.com]
Language is froth on the surface of thought
Similar Threads
-
Calling a method in another class
By uncopywritable in forum New To JavaReplies: 9Last Post: 10-22-2012, 04:01 PM -
Accessing boolean Values of another values in one class.
By a_iyer20 in forum Advanced JavaReplies: 4Last Post: 04-15-2008, 01:04 PM -
Calling method from another class
By asahli in forum New To JavaReplies: 1Last Post: 12-15-2007, 06:24 PM -
Problem calling another class
By adlb1300 in forum New To JavaReplies: 3Last Post: 10-25-2007, 02:05 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks