Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Callling one class from another class

Posted on 2003-03-13
34
Medium Priority
?
177 Views
Last Modified: 2010-03-31
I am currently coding an applet which has a main applet with 5 panels on it. each of the panels are seperate classes extending panel. now heres an example of what i cant work out what to do.



public class MainProg extends Applet
{
public Tape = new Tape()                  //this is a class extending panel
public ControlPanel = new ControlPanel()  //this is a class extending panel

public void init()
{
add(Tape);
add(ControlPanel);
}
}


public class Tape extends Panel
{
   //from inside this class i want to call MainProg.ControlPanel.setState(1);  
}

public class ControlPanel extends Panel
{
   public void setState(int state)
   {
   }
}


the way stated above tells me 'non-static variable ControlPanel cannot be referenced from a static context'. How can i call this method???

any help will be greatly appreciated
0
Comment
Question by:Theking123
  • 14
  • 13
  • 7
34 Comments
 
LVL 1

Expert Comment

by:pronane
ID: 8129490
where you are calling Controlpanel either make the method static, that you are calling it from or else created an instance of the method like such:

Controlpanel p = new Controlpanel();

and use p as the instance of the object

Paul
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129524
U didnt say what class was giving you the error, but if it is set state  just make set state static like this:

public static void setState(int state)
  {
  }
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129547
Is this the line that gives you the error?

MainProg.ControlPanel.setState(1);  

either of the things i said will work, easiest is just to make the setstate method static like above.


0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 

Author Comment

by:Theking123
ID: 8129551
the error was coming from the other class not the setState class from the calling class. and i dont want to instantiate a second object i want to use the one already constructed which has a panel and is drawing on it. If i make another i would draw on the wrong one.
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129564
ya well then just make the setState method static like i said
i.e
public static void SetState()
{
}
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129572
that will work fine, I have tried it my side
0
 

Author Comment

by:Theking123
ID: 8129589
the line that gives the error is
MainProg.ContolPanel.setState(1);

0
 
LVL 1

Expert Comment

by:pronane
ID: 8129612
ya make the method setState static, have you tried that?
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129631
I should have explained it better, when you are calling a non-static method ( like in your case setState is a non static method) in a static context it will give that error, so there are two ways to get around it one is to make the method you are calling static , or else the instance of the class,
so where you go
public ControlPanel = new ControlPanel()  
make that call static.

Am I making sense to you??  If not please say so and I will try to explain better
0
 

Author Comment

by:Theking123
ID: 8129635
i am still getting the error after making the public void setState(int state)

public static void setState(int state)

on the class with the MainProg.ControlPanel.setState(1) line.

and aswell as that error i gain non static variables that cannot be referenced and my repaint() cannot be referenced

is there a way without making it static
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 2000 total points
ID: 8129647
I would do something like this:

public class MainProg extends Applet
{
  Tape tape;
  ControlPanel controlPanel;
 
 

public void init()
{
 
  controlPanel = new ControlPanel();
  tape = new Tape();
  add(Tape);
  add(ControlPanel);
  tape.setControlPanelState(1);
}

}


public class Tape extends Panel
{
  //from inside this class i want to call MainProg.ControlPanel.setState(1);  
 
  ControlPanel controlPanel;
 
  public Tape() {
    super();
  }
 
  public Tape(ControlPanel controlPanel) {
    this.controlPanel = controlPanel;
  }
 
  public void setControlPanelState(int state) {
    controlPanel.setState(state);
  }
 
}

public class ControlPanel extends Panel
{
 
  int state;
 
  public void setState(int state)
  {
    this.state = state;
  }
}
0
 

Author Comment

by:Theking123
ID: 8129657
but when i start making things static i am unable to call my browsers repaint() method as it is a non-static method ?
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129667
No, because you are trying to call the method from a static context, so therefore if it is not static it will give that error, that is strange, can you post the whole code with the imports nad I will run it here?  Did u trying setting the ControlPanel p = new Controlpanel() at the start or from within the Tape class and call it like this in the class:

p.setState(1);


Thanks,

Paul
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129675
ya Cejh said like i had said earlier on in the post, that will work also what he said.
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129683
hence:

"where you are calling Controlpanel either make the method static, that you are calling it from or else created an instance of the method like such:

Controlpanel p = new Controlpanel();

and use p as the instance of the object"

Paul
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8129686
Not a good idea to have *any* static methods in applets particularly.
0
 

Author Comment

by:Theking123
ID: 8129699
would that method work if instead of 2 classes i wanted to communicate between there was 5 and i wanted them to be able to call methods in both directions?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8129721
Well if you made the inner classes of the applet everything would be in scope, but the points is, you should be engineering a solution that does not have all these circular dependencies.
0
 

Author Comment

by:Theking123
ID: 8129768
well what i actually have is 1 applet with 5 panels on it
but i need the 5 panels to call each others methods.

i have a table_of_instructions panel which sets / reads instruction lines of data in a table

i have a controlpanel which tells a tape to start/stop/reset etc

and the tape asks the table_of_instructions for data does its relevant thing and reports its state to control_panel which displays it

but i cannot fathom out how to call the panels methods from the other panels.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8129831
In that case, a simple way to do it is to use your applet as a 'mediator' between the various panels. Pass a reference to the applet in each Panel subclass constructor. Have methods in your applet such as:

getTapePanel()

getControlPanel

getXPanel()

that will return the appropriate reference when a panel needs a reference to one of its 'fellows'
0
 

Author Comment

by:Theking123
ID: 8129874
could you possibly show me an example as i dont seem to be able to implement it.
0
 
LVL 1

Expert Comment

by:pronane
ID: 8129906
he means ( I think )

public getTapePanel()
{
      return tapepanel  // where that is the instance of the class
}
0
 

Author Comment

by:Theking123
ID: 8129909
i have tries

public class MainProg()
{
Tape tape = new Tape();
ControlPanel controlPanel = new ControlPanel();

public void init()
{
add(Tape);
add(ControlPanel);
}

public ControlPanel getControlPanel()
{
return controlPanel;
}
}

public class Tape extends Panel
{
ControlPanel cp;

public Tape()
{
cp = MainProg.getControlPanel();
}
}
0
 

Author Comment

by:Theking123
ID: 8129936
i will paste the main applet so you can fully see what i am getting at.


import java.awt.*;
import java.applet.*;

public class TuringMachine extends Applet
{
     
     public Tape SimulatorTape = new Tape();
     public ControlPanel SimulatorControlPanel = new ControlPanel();
     public TableOfInstructions  SimulatorTableOfInstructions = new TableOfInstructions();
     
     public void init()
     {
          setLayout (null);
               
          SimulatorTape.setLocation(10, 212);
          add(SimulatorTape);
         
          SimulatorControlPanel.setLocation(10, 281);
          add(SimulatorControlPanel);
         
          SimulatorTableOfInstructions.setLocation(10, 315);
          add(SimulatorTableOfInstructions);
     }
}    


and what i want is from the code inside these objects to be able to call methods inside other ones of these objects
e.g simulator tape call tableofinstructions and controlpanel and controlpanel call tableodinstructions and tape etc:
0
 
LVL 1

Expert Comment

by:pronane
ID: 8130035
So what you are saying is that you want to try to call a class from an instance of another class i.e (if this were possible)  essentially:

Controlpanel p = new Controlpanel();
public Tape SimulatorTape = new Tape();

p.SimulatorTape.MethodInClassTape();  

so that whatever the result of the methodInclassTape was would be called by p ( the instance of the Controlpanel class)
0
 

Author Comment

by:Theking123
ID: 8130061
no we have 1 OUTER CLASS(applet) holding 5 INNER CLASSES(panels) we want the INNER CLASSES to call methods from other INNER CLASSES(panels) through the OUTER CLASS(applet)
0
 
LVL 1

Expert Comment

by:pronane
ID: 8130108
You should just be able to do this then: lets take the class tape example and controlpanel class right:

Controlpanel c;
Tape t;
Example p;

class Tape
{
c = new Controlpanel();
  public void example()
  {
    c.work();
  }
  public void callTape()
  {
   
   }
}



class controlpanel
{
  t = new tape();

   public void work()
   {
    //do stuff here
   }
   public void callControl()
   {
      t.callTape();
   }
}

If this was your question then this will work fine.

0
 
LVL 1

Expert Comment

by:pronane
ID: 8130144
oh sorry, meant to finish that, dont know where me head is.

Have the those methods return something so that a call can be invoked froom the outer class so that the results of these method calls can be stored and used/manipulated by the outer class.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8130190
If you do something like this, filling out the methods of the classes as necessary, they will each be able to call each other's methods:



import java.awt.*;
import java.applet.Applet;

public class MainProg extends Applet
{
  TapePanel tape;
  ControlPanel controlPanel;



  public void init()
  {
    controlPanel = new ControlPanel();
    tape = new TapePanel();
    add(tape);
    add(controlPanel);
  }



  abstract class TuringPanel extends Panel {

    public TuringPanel() {
      super();
    }

  }

  class ResetPanel extends  TuringPanel {
  }

  class ControlPanel extends TuringPanel {
  }

  class TapePanel extends TuringPanel {
  }

}


0
 

Author Comment

by:Theking123
ID: 8130319
we have fully implemented just as you have said
but cannot reference the other inner classes.

are we trying to reference correct?

we have tried
   from inside the tape class
       controlPanel.myMethod();
       super.controlPanel.myMethod();
       TuringMachine.controlPanel.myMethod();

the only one that even finds the method is TuringMachine.controlPanel.myMethod(); but returns non-static errors?
0
 

Author Comment

by:Theking123
ID: 8130338
thanks for your help so far i feel were nearly there but i will return at about 11:53 am pst because i have to do something. sorry
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8130577
Should be no 'static' anywhere
0
 

Author Comment

by:Theking123
ID: 8132090
Thank you very much for the help. after careful testing we have passed the main applet using the this keyword to an instantiation variable in the panel classes and are using the main applet as a mediator for the method calling. so we basically just worked out what you said earlier on. spot on. thanks again enjoy your well deserved 500 points for putting up with us for hours :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8132127
That's OK. I'm glad you went that way instead of the 'free for all' way!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

571 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question