LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   java class files (http://www.linuxquestions.org/questions/programming-9/java-class-files-444160/)

trscookie 05-12-2006 12:43 PM

java class files
 
right im in a bit of a predicament i have one network class file that is used to connect to a host and another class file that is used to make the logon form for my client im using sockets what im trying to do is make the network class available for the other forms so i dont have to induce the network everytime the form changes.

so what i have at the moment is my network function:

Code:

public static void connect( String ip, int port ) { }
however the problem with static functions is that they cant see public variables now i want one form to be able to change the ip address and port, so i tried making a chage ip function but you cant view it from another class unless its staic as far as im awhere and you can only have it so you can either read the variable or write to a variable if its not for example:

Code:

class bob {

public void myFunction() {
  otherClass.times( x );
  System.out.println( otherClass.times() );
}

}


Code:

class otherClass() {
  public static String times( String x ) {
    return x * x;
  }
}


another example of this would be to try and use global variables is there any way to do this using static functions? cheers alot.


i understand that this code as it is wont work, im just using it as an example, please help, cheers trscookie.

graemef 05-12-2006 03:10 PM

I think that you want a singleton class, for your connection. Do a google for singleton and you should get plenty of examples to work with.

spooon 05-12-2006 03:45 PM

I'm not sure exactly what you are trying to do; but perhaps you should look up some of the concepts of object-oriented programming. I am not sure if you are using static functions and variables properly.

If the network connection is specific to a form, then you might want to make a network object as a variable in the logon-form class, and then you shouldn't use static things at all.

On the other hand, if there is just one network connection that is "global" across whole program, then you should make everything in the network class static.

Quote:

Originally Posted by trscookie
however the problem with static functions is that they cant see public variables

static functions are outside of any instance, so they cannot access any instance variables (i.e. anything that is not static).

Quote:

Originally Posted by trscookie
so i tried making a chage ip function but you cant view it from another class unless its staic as far as im awhere and you can only have it so you can either read the variable or write to a variable if its not for example:

Code:

class bob {

public void myFunction() {
  otherClass.times( x );
  System.out.println( otherClass.times() );
}

}


Code:

class otherClass() {
  public static String times( String x ) {
    return x * x;
  }
}


You can also do stuff like this (create an instance and use its methods); but note that it is doing a completely different thing:
Code:

class bob {

public void myFunction() {
  otherClass foo;
  foo.value = 5;
  System.out.println( foo.printValue() );
}

}

class otherClass() {
  public int value;
  public int printValue() {
    return value;
  }
}

[/QUOTE]


Quote:

Originally Posted by trscookie
another example of this would be to try and use global variables is there any way to do this using static functions? cheers alot.

static variables are "global", but I'm not sure you need to use them

paulsm4 05-12-2006 03:58 PM

trscookie -

I agree with spooon that maybe "static" isn't the best solution for your particular problem, and with graemef that, if you do in fact need "static", then the "singleton" design pattern is probably appropriate - and that you can find lots of Java implementation examples of "singleton" without too much trouble.

And unfortunately, you completely lost me with the "foo" and "bob" examples.

Sooooooooo -

Could you please help us out and answer these three questions:

1. Why do you think you need "static"?
<= There can be many good reasons ... we just need to know what you're doing and
why "static" is important for this particular problem

2. Who needs to access data in your static method?
<= It sounds like other classes just *call* your static method: they don't
necessarily read any data from it


3. Who does your static method need to access data from?
<= It sounds like this is the problem

I suspect the problem is "3)".

And perhaps you can solve the problem just by adding a parameter, and passing a reference into your static method.

Just a thought .. PSM

trscookie 05-12-2006 06:42 PM

Hello all cheers for all of the replies. sorry for using foo and bob as examples i was in a bit of a hurry. ill try and give more information this time.

i have three classes "classConnect", "frmLogon" and "frmConnect". in previous examples i was trying to use smaller classes to avoid confusion but ended up making it worse ;).

the "classConnect" has the code to connect to the network:

Code:

public void connect( String ip, int port ) { ... }
frmLogon: doesnt really have code at the moment but is going to use the classConnect.connect function to access the server. i want the classConnect in a seperate function as i will have a frmLogon and a frmMain that will both need to use the send function in the classConnect class. still with me?!


the other form frmNetwork changes the IP and port number before the connection is made, however i cant get the forms( frmNetwork and frmLogon ) to view the functions in classConnect unless i declare it as a static for some reason. i.e. if i was to try and use the function above and try and call that from frmLogon it wouldnt see it, but if i declare it

Code:

public static void connect( String ip, int port ) { ... }
when i type frmLogon the dropdown shows "connect".


have i just made more confusion? cheers for all the help.

paulsm4 05-12-2006 07:09 PM

OK - I'm with you so far...

What's the problem with making your "connect" method static?
Code:

public static void connect( String ip, int port ) { ... }
  <= Is there any reason this won;t work?

What exactly does "connect()" use that *isn't* static? For example, if "connect()" happens to use some member data - could you simply *make* that member data static?

trscookie 05-12-2006 07:31 PM

i wanted to have a default port and ip address declared at the top of the class so a. other classes could see what the ip address and port number had been changed to:

String ip = "127.0.0.1";
int port = 8189;

however i get:

Code:

client/classConnect.java [16:1] non-static variable socket cannot be referenced from a static context
            socket = new Socket( ip, port );

...error from the compiler cheers

trscookie 05-13-2006 10:34 AM

ok here is the full thing( classConnect ):

Code:

    public static String connect( String ip, int port, String Username, String Password ) {
        String newState = "";
        Socket socket = null;
        PrintWriter out = null;
        BufferedReader in = null;         
       
        try {
            socket = new Socket( ip, port );
            out = new PrintWriter( socket.getOutputStream(), true );
            in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
            newState = ( "State: Connected to: " + ip + ": " + port );
            //lblState.setText( "State: Connected" );
        } catch( UnknownHostException errNoHost ) {
            newState = ( "State: Could not locate host! " + String.valueOf( ip ) + " " + String.valueOf( port ) );
            System.err.println( "State: Could not locate host! " + String.valueOf( ip ) + " " + String.valueOf( port ) );
            //System.exit( 1 );
        } catch( IOException errIO ) {
            newState = ( "State: Could not I/O with server check permissions and make sure the server has started." );
            System.err.println( "State: Could not I/O with server check permissions and make sure the server has started." );
            //System.exit( 1 );
        }
       
               
        return newState;
    }
   
   
    public static String send( String dataToSend ) {
        BufferedReader in = null;
        in  = new BufferedReader( new InputStreamReader( connect() ) );
        String value, fromServer, fromClient;
       
        while( ( fromServer = in.readline() ) != null ) {
        value = fromServer;
        if( fromServer.equals( "close." ) ) { value = "Connection closed."; break; }
       
        fromClient = dataToSend;
        if( fromClient != null ) {
          System.out.println( dataToSend );
          out.println( fromClient );
        }
        }
       
        return value;
    }


right and the problem lies in the buffer reader if i declare it as a static the input wont come in from the server. there is another class called frmLogin that will access the classConnect:

i wanted to have it so i could just type something like this:

classConnect.connect( 192.168.0.1, 2143 );

then to send data just type:

classConnect.send( "datatosend" );

then to receive information the send function would automatically retreive the data and and display it in label. is that better explained? cheers trscookie.

spooon 05-13-2006 03:25 PM

So I don't think you need to use anything static. In OO, you can use instance variables to store state. You probably want something like this:

Code:

public class classConnect {
    public Socket socket;
    public PrintWriter out;
    public BufferedReader in;         

    public String connect( String ip, int port, String Username, String Password ) {
        String newState;
       
        try {
            socket = new Socket( ip, port );
            out = new PrintWriter( socket.getOutputStream(), true );
            in = new BufferedReader( new InputStreamReader( socket.getInputStream() ) );
            newState = "State: Connected to: " + ip + ": " + port;
            //lblState.setText( "State: Connected" );
        } catch( UnknownHostException errNoHost ) {
            newState = "State: Could not locate host! " + ip + " " + port ;
            System.err.println( "State: Could not locate host! " + ip + " " + port );
            //System.exit( 1 );
        } catch( IOException errIO ) {
            newState = "State: Could not I/O with server check permissions and make sure the server has started.";
            System.err.println( "State: Could not I/O with server check permissions and make sure the server has started." );
            //System.exit( 1 );
        }
       
               
        return newState;
    }
   
   
    public String send( String dataToSend ) {
        String value, fromServer, fromClient;
       
        while( ( fromServer = in.readline() ) != null ) {
        value = fromServer;
        if( fromServer.equals( "close." ) ) { value = "Connection closed."; break; }
       
        fromClient = dataToSend;
        if( fromClient != null ) {
          System.out.println( dataToSend );
          out.println( fromClient );
        }
        }
       
        return value;
    }
}


and when you want to send stuff:

Code:

classConnect foo = new classConnect;
foo.connect( 192.168.0.1, 2143 );
foo.send( "datatosend" );


trscookie 05-13-2006 05:06 PM

so i was on the right tracks just not properly understanding it then....? cheers


All times are GMT -5. The time now is 03:29 PM.