Problem with static/non-static functions
Hey !
I'm developing in Netbeans and I encountered this problem today: I am using my main class to create te interface and this class is ran by default when I launch the .jar.
It looks like this:
public class Main extends JFrame {
Thread s = null;
public Main() {
super("interface");
try{
UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel());
}catch(Exception e){JOptionPane.showMessageDialog(null, e.toString());}
initComponents();
this.setVisible(true);
}...[more functions.. that doesn't matter]
}
I have a function in Main that should add some stuff to my frame, depending of what it recieves from another class that extends Thread, and looks like this:
class Server extends Thread{
int Port, i;
ServerSocket server;
Socket sock;
public Server(){
super("Nodens");
this.Port = 6363;
this.i=0;
}...[more functions]
}
My problem is that i don't know how to invoke a function in the Main class from the Server class, because I don't have a runtime name for it [ as it is ran by the jar's manifest ] and if I try Main.function() it throws me this error:
non-static method cannot be referenced from a static context
And if I will instanciate Main for this, it will create a new frame [ window ] and it's not what I want.. anyway.. in the new window, changes are taking place, of course, because the new one is a non-static environment.
Maybe anyone has any idea of how would I resolve this problem,
Thanks!
[1746 byte] By [
bedeabza] at [2007-11-11 7:25:40]

# 1 Re: Problem with static/non-static functions
The traditional way to solve this problem is by using the singleton design pattern. Essentially, it would look something like this:
public class Main extends JFrame {
public static final Main SINGLETON = new Main();
Thread s = null;
private Main() {
super("interface");
try{
UIManager.setLookAndFeel(new com.sun.java.swing.plaf.windows.WindowsLookAndFeel ());
}catch(Exception e){JOptionPane.showMessageDialog(null, e.toString());}
initComponents();
this.setVisible(true);
}...[more functions.. that doesn't matter]
}
Then you could access a non-static function by saying.
Main.SINGLETON.function();
There are other ways to implement this pattern but I personally think this is the easiest one. Note that this means that you can only have 1 instaniation of the Main class (that's the reason this pattern is called the "singleton" pattern). If you want to know more about patterns, you can look them up on wikipedia, its a pretty good resource.
Hope this helps.
evlich at 2007-11-11 22:38:24 >

# 2 Re: Problem with static/non-static functions
Yes, this makes a new frame that can be referenced from Server class, but I have another problem now: I have 2 frames [ the initial one and the SINGLETON one ] and my changes are visible on the initial one and the SINGLETON one acts more like a dummy object. If I try to hide the SINGLETON with
this.SINGLETON.setVisible(false);
the server doesn't start anymore.
If I try to hide the initial frame, I don't see the changes anymore, because they affect the initial frame.
Could you explain me a little more what's happening with this method ?
Thanks!
# 3 Re: Problem with static/non-static functions
When using the singleton pattern, you should never subclass or instantiate an object of type Main. You should only use the SINGLETON variable.
evlich at 2007-11-11 22:40:22 >

# 4 Re: Problem with static/non-static functions
Yes, but the singleton variable is in class Main, and if the Main class won't be instanciated at least once, there wouldn't exist any singleton...
# 5 Re: Problem with static/non-static functions
Not quite, since the SINGLETON variable is declared static, Java will initialize it when it initializes the variable which will call the constructor.
evlich at 2007-11-11 22:42:31 >

# 6 Re: Problem with static/non-static functions
Yes, but tell me how should I keep only one frame visible [ the main window is doubled ] and changes that I make trough Main.SINGLETON.function() still be visible on the interface.
I attached the .java file of my Main class.
Thanks very much !
# 7 Re: Problem with static/non-static functions
Here, give this a try.
evlich at 2007-11-11 22:44:34 >

# 8 Re: Problem with static/non-static functions
you need to make the constructor private otherwise it's not a singleton. Also if you need to access methods from Main in the Server then why not create Server in Main like
Server s;
private void startNodens(){
Control.setText("Stop Nodens");
LED.setBackground(new java.awt.Color(0, 204, 51));
ComStat.setText("Alive...");
setStatus("Alive... waiting for clients");
log("Server alive...");
s = new Server(this);
s.start();
this.getInstance().setVisible(false);
}
private void stopNodens(){
Control.setText("Start Nodens");
LED.setBackground(new java.awt.Color(204, 0, 51));
ComStat.setText("Dead");
setStatus("Dead");
log("Server dead.");
s.stop();
}
class Server extends Thread{
int Port, i;
ServerSocket server;
Socket sock;
final Main frame;
public Server(Main frm){
super("Nodens");
frame = frm;
this.Port = 6363;
this.i=0;
}
public void run(){
try{
server = new ServerSocket(Port);
while(true){
System.out.println("Waiting for client...");
sock = server.accept();
ClientThread t = new ClientThread(sock,i);
t.start();
SwingUtilities.invokeLater(new Runnable(){
public void run(){
frame.addClient(i);
}}); //got to avoid event thread issues
i++;
System.out.println("Numar de clienti: "+ i);
}
}catch(IOException e){
System.err.println("IO Error: \n"+e);
}finally{
try{
server.close();
}catch(Exception e){}
}
}
# 9 Re: Problem with static/non-static functions
Thank you very much, everyone.
It worked both ways :) but I have chosen the inner class one, because it seemed simplier.
Regards, Bedeabza