Calling a method from another class while in a seperate class

Hello
I am currently TRYING to proramme a simple alarm clock program, The way it has had to be set out is that there are two classes one is called ClockDisplay which is just the clock and the one i have had to create is called AlarmDisplay
I am trying to write code so that when the String alarmdisplayString(this is located in the alarm class which i am typing this code) is the same as String displayString the system will print out something like "Beep wake up" So far i have this code but i cannot figue out what to put to reference the method from the ClockDisplay class while codingi n the AlarmDisplay class
if(alarmdisplayString = DONT KNOW WHAT TO PUT HERE) {
system.out.println ("DING WAKE UP");
Any help would be great!
Thanks

That makes sense i have put in this code
private void Alarmtone()
ClockDisplay displayString = new ClockDisplay();
if (alarmdisplayString == ClockDisplay.displayString){
system.out.println ("DING WAKE UP");
displayString is the method in the ClockDisplay Class
alarmdisplayString is the method in the AlarmDisplay class
I get the error message "non-static variable displayString cannot be referenced from a static context
Thanks for the help so far :D

Similar Messages

  • How to call a method from another class

    I have a problem were i have to call a method from another class. What is the command line that i have to use. Thanks.

    Here's one I wipped up in 10 minutes... Cool!
    package forums;
    import javax.swing.JFrame;
    import javax.swing.JTextArea;
    import krc.utilz.io.Filez;
    import java.io.FileNotFoundException;
    class FileDisplayer extends JFrame
      private static final long serialVersionUID = 0L;
      FileDisplayer(String filename) {
        super(filename);
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);
        this.setSize(600, 800);
        JTextArea text = new JTextArea();
        try {
          text.setText(Filez.read(filename));
        } catch (FileNotFoundException e) {
          text.setText(e.toString());
        this.add(text);
      public static void main(String args[]) {
        final String filename = (args.length>0 ? args[0] : "C:/Java/home/src/forums/FileDisplayer.java");
        try {
          java.awt.EventQueue.invokeLater(
            new Runnable() {
              public void run() {
                new FileDisplayer(filename).setVisible(true);
        } catch (Exception e) {
          e.printStackTrace();
    Filez.read
       * reads the given file into one big string
       * @param String filename - the name of the file to read
       * @return the contents filename
      public static String read(String filename) throws FileNotFoundException {
        return Filez.read(new FileReader(filename));
       * Reads the contents of the given reader into one big string, and closes the reader.
       * @param java.io.Reader reader - a subclass of Reader to read from.
       * @return the whole contents of the given reader.
      public static String read(Reader in) {
        try {
          StringBuffer out = new StringBuffer();
          try {
            char[] bfr = new char[BFRSIZE];
            int n = 0;
            while( (n=in.read(bfr,0,BFRSIZE)) > 0 ) {
              out.append(bfr,0,n);
          } finally {
            if(in!=null)in.close();
          return out.toString();
        } catch (IOException e) {
          throw new RuntimeIOException(e.getMessage(), e);
      }Edited by: corlettk on Dec 16, 2007 1:01 PM - dang [code [/tags][                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                   

  • Calling a method from another class... that requires variables?

    I'm calling a method from another class to change the date (its a date object) in another class.
    I keep getting the error 'setDate(int,int,int) in Date cannot be applied to ()'
    My code is:
    public int changeDate()
         cycleDate.setDate();
    }I'm guessing I need to pass 3 parameters (day, month, year), but I'm not sure how! I have tried, but then I get errors such as ')' expected?
    Any ideas! :D

    f1d wrote:
    I'm calling a method from another class to change the date (its a date object) in another class.
    I keep getting the error 'setDate(int,int,int) in Date cannot be applied to ()'
    My code is:
    public int changeDate()
    cycleDate.setDate();
    }I'm guessing I need to pass 3 parameters (day, month, year), seems that way from the error you posted
    but I'm not sure how!
    setDate(16, 6, 2008);
    I have tried, but then I get errors such as ')' expected?
    Any ideas! :Dyou need to post your code if you're getting specific errors like that.
    but typically ')' expected means just that, you have too many or not enough parenthesis (or in the wrong place, etc.)
    i.e. syntax error

  • Help on Calling a method from another class

    how can i call a method from another class.
    Class A has 3 methods
    i just want to call only one of these 3 methods into my another class.
    How can I do that.

    When i am trying this
    A a=new A;
    Its calling all the methods from class A. I just want
    to call a specfic method.How can it be done?When i am trying this
    A a=new A();
    Its calling all the methods from class A. I just want to call a specfic method.How can it be done?

  • How do you call a method from  another class without extending as a parent?

    How do you call a method from another class without extending it as a parent? Is this possible?

    Why don't you just create an instance of the class?
    Car c = new Car();
    c.drive("fast");The drive method is in the car class, but as long as the method is public, you can use it anywhere.
    Is that what you were asking or am I totally misunderstanding your question?
    Jen

  • Calling a method from another file

    This is pretty basic stuff but i can't seem to get it right. I am calling a method from another file. The other file IS located in the same folder BUT when i compile i get errors
    "cannot find symbol" <===referring to limit and sieve i believe.
    The method name is "sieve" the file name is "PrimeSieve2008" and "limit" is the variable in brackets in the real method.
         public static void main (String [] args) {
    final int [] PRIMES;
    int sieve = PrimeSieve2008.sieve(limit);
         PRIMES = sieve(getValidInt());
              for (int j = 0; j<PRIMES.length; j++) {
                   System.out.println("Prime[" + j + "] = " + PRIMES[j]);
    Is "int sieve = PrimeSieve2008.sieve(limit)" the wrong way to call a file?
    Thanks a million,
    Alex
    Edited by: Simplistic2099 on Apr 3, 2008 7:47 PM
    Edited by: Simplistic2099 on Apr 3, 2008 7:49 PM

    Simplistic2099 wrote:
    the other method runs fine:
    "public static int[] sieve(final int limit){
    int candidate; // possible prime
    int count; // no. of primes found
    boolean[] mayBePrime = new boolean[limit+1];
    // remaining possibilities
    final int[] PRIMES; // array to return
    // initialize mayBePrime
    for ( int j = 0 ; j <= limit ; j++ ) {
    mayBePrime[j] = true;
    mayBePrime[0] = mayBePrime[1] = false;
    // apply sieve, and count primes
    candidate = 2;
    count = 0;
    while ( candidate <= limit ) {
    if ( mayBePrime[candidate] ) {
    count++;
    for ( int j = 2 * candidate ; j <= limit ; j += candidate ) {
    mayBePrime[j] = false;
    } // end for
    } // end if
    candidate++;
    } // end while
    // fill up new array with the primes found
    PRIMES = new int[count];
    count = 0;
    for (int j = 2 ; j <= limit ; j++ ) {
    if ( mayBePrime[j] ) {
    PRIMES[count] = j;
    count++;
    } // end if
    } // for
    return PRIMES;
    } // sieve
    I really am clueless here.in this one you are passing in limit.
    in the other one you are getting limit from somewhere outside of main.

  • Can't add list element when calling a method from another class

    I am trying to call a method in another class, which contains code listmodel.addElement("text"); to add an element into a list component made in that class.
    I've put in System.out.println("passed"); in the method just to make sure if the method was being called properly and it displays normally.
    I can change variables in the other class by calling the method with no problem. The only thing I can't do is get listmodel.addElement("text"); to add a new element in the list component by doing it this way.
    I've called that method within it's class and it added the element with no problem. Does Java have limitations about what kind of code it can run from other classes? And if that's the case I'd really like to know just why.

    There were no errors, just the element doesnt get added to the list by doing it this way
    class showpanel extends JPanel implements ActionListener, MouseMotionListener {
           framepanel fp = new framepanel();
           --omitted--
         public void actionPerformed(ActionEvent e){
                  if(e.getSource() == button1){
                       fp.addLayer();
    /*is in a different class file*/
    class framepanel extends JPanel implements ActionListener{
            --omitted--
         public void addLayer(){
              listmodel.addElement("Layer"+numLayer);
              numLayer++;
    }

  • Calling repaint method from another class

    My question in a very simple form :
    how do I call repaint mathod from another class.
    e.g: Let's say class "A.java" is a JFrame .
    Class "B.java" is a JPanel which is added to the JFrame above.
    Class "C.java" is a JDialog containing some JButtons.
    How do I call the repaint method from the class "C.java".
    Thank you in advance!!

    My question in a very simple form :
    how do I call repaint mathod from another class.
    e.g: Let's say class "A.java" is a JFrame .
    Class "B.java" is a JPanel which is added to the JFrame above.
    Class "C.java" is a JDialog containing some JButtons.
    How do I call the repaint method from the class "C.java".
    Thank you in advance!!

  • Calling a method from another class or accessing a component from another

    Hi all
    im trying to make a find/replace dialog box
    my main application form has a jtextpane and when i open up the find and replace dialog box it has two textboxes (find and replace)
    now i have the code to do the finding on my jtextpane but how do i call that code to do the find method?
    I have tried having the code in my main application class but then how do i call that method from my dialog box class?
    ive also tried having the code in my dialog box class, but then how to i tell it to work on my jtextpane which is in my main ap class?

    well if someone had been nice enough to provide me
    with a tutorial i wouldnt have gotten into this
    muddle, no need to be rude is there!I'm not rude. And you also wouldn't have gotten into the muddle if you searched yourself. This site provides many very good tutorials about all kinds of stuff.
    http://java.sun.com/docs/books/tutorial/java/javaOO/classes.htmlAmong other things, it mentions that "static" defines everything that belongs to a class, as opposed to an object.

  • ABAP Objects : calling one method from another class

    Hi,
    Can you please tell me how to call method from one class or interfce to another class.The scenario is
    I have one class CL_WORKFLOW_TASK, this class have interface IF_WORKFLOW_TASK & this interface have method IF_WORKFLOW_TASK~CLOSE. Now my requirement is ,
    There is another class CL_WORKFLOW_CHAIN ,this class have interface IF_WORKFLOW_CHAIN & this interface have method IF_WORKFLOW_CHAINCLOSE_ALL_PREDECESSORS. Now i have to write my code in this method but i have to use IF_WORKFLOW_TASKCLOSE method for closing the task.
    Can you please give me the code for the above .
    Please waiting for reply.

    Hi,
    You can use the concept of INHERITANCE  in this scenario.By using this concept, you can call all the public and protected  methods of class CL_WORKFLOW_TASK  in the required calss CL_WORKFLOW_CHAIN as per your requirement.
    Go through the  Introdctory(INHERITANCE) programming from this SAPHELP link.
    http://help.sap.com/saphelp_nw70/helpdata/en/1d/df5f57127111d3b9390000e8353423/content.htm
    I hope, it will help in you inresolving your problem.
    by
    Prasad GVK.

  • Simple enough but... calling a method from another class

    Hi all,
    I know it's simple enough, although I can't see where I'm going wrong for some reason. Basically I've got a class which extends JPanel but when I try to call one of its methods from my main class I get an error. The relevant code (I hope) is:
    Main.java
    import java.awt.BorderLayout;
    import javax.swing.UIManager;
    public class Main extends JFrame implements ActionListener {
         JFrame frame;
         public static JPanel statusBar;
         public Main() {
              // Add a status bar
              statusBar = new StatusBar();
              mainPanel.add( statusBar, BorderLayout.SOUTH );
              setContentPane( mainPanel );
         public static void setStatusBarText( String s ) {
              statusBar.setStatusText("Test");
    StatusBar.java
    import java.awt.Color;
    import javax.swing.SwingConstants;
    public class StatusBar extends JPanel {
         private int barWidth;
         private static JLabel status;
         public StatusBar() {
              super();
              //barWidth = Main.getProgramWidth();
              //setPreferredSize( new Dimension(barWidth,22) );
              setLayout( new FlowLayout( FlowLayout.LEADING ) );
              setBorder( BorderFactory.createMatteBorder(1, 0, 0, 0, new Color(160,160,160) ) );
              status = new JLabel("Test", SwingConstants.LEFT);
              //status.setVerticalAlignment( SwingConstants.CENTER );
              add( status );
         protected void setStatusText( String s ) {
              status.setText( s );
              revalidate();
    }The "statusBar.setStatusText("Test");" call in the main class file doesn't work and I get the error:
    >
    cannot find symbol
    symbol : method setStatusText(java.lang.String)
    location: class javax.swing.JPanel
    statusBar.setStatusText("Test");
    >
    Any ideas what I'm doing wrong? I created an instance of the StatusBar class called statusBar, then I use this to call the setStatusText() method (via statusBar.setStatusText()) which I thought was all correct, then it doesn't work? Even if there's an easier way to do what I want to achieve (i.e. update a JLabel from the 'central' main class file instead of setting the JLabel to static and having every class call it directly), I'd appreciate knowing where I'm going wrong here nonetheless.
    Many thanks,
    Tristan

         public static JPanel statusBar;
         public static void setStatusBarText( String s ) {
              statusBar.setStatusText("Test");
    >
    cannot find symbol
    symbol : method setStatusText(java.lang.String)
    location: class javax.swing.JPanel
    statusBar.setStatusText("Test");
    >The type of variable statusBar is JPanel. That's all the compiler takes into account. It's irrelevant that at some point in the program the type of the object pointed to by this variable is a subclass of JPanel (because at another point it might be an instance of another subclass, or of JPanel itself.

  • How call a method from another class

    i make three class.class A,class B and class C.
    class A have One button.
    Class B have a action listener of that button.
    class c have a metod like
    public void test(){     }
    how can i call a method in class b from class c;
    is it necessary to pass the class a or b through the constructor of class c or another way to call the method.

    public class Foo
        public static void main(String[] args)
            Bar.staticFn();
            Bar b = new Bar();
            b.memberFn();
    class Bar
        public void memberFn()
            System.out.println("memberFn");
        public static void staticFn()
            System.out.println("staticFn");
      }

  • Calling a method from another class. Please Help!!

    Here is my full program. Like I said in my last post. I am having trouble calling the baseMakesError1 method. This method is called in the subcallsbase1 method. Also, it would be of great help if you could look for any other errors with with my program. Thank you so much. I really appreciate it.
    import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    public class ThomasR11
    String sy;
    public static void main(String[] args)
    System.out.println("(main): Ryan Thomas, file ThomasR11: Exceptions");
    SubRT obj3;
    SubRT obj4;
    String sX;
    String x;
    String s;
    String sY;
    try
    ThomasR11 obj1=new ThomasR11();
    catch(NullPointerException NPE)
    System.err.println("Null Pointer Exception");
    ThomasR11 obj2=new ThomasR11("Hello");
    System.out.println("This is the last statement in main");
    public ThomasR11()
    SubRT obj3= new SubRT();
    obj3.subCallsBase1();
    public ThomasR11(String sY)
    throws FileNotFoundException
    SubRT obj4= new SubRT();
    obj4.subError2();
    obj4.subError3(sY);
    private static class BaseRT
    public BaseRT()
    System.out.println("In BaseRT constructor= " + this);
    public void baseMakesError1(int value0)
    System.out.println("In BaseRT constructor, about to throw exception");
    if (value0==0)
    throw new NullPointerException("NullPointerException");
    private class SubRT extends BaseRT
    SubRT subNull;
    public SubRT()
    System.out.println("In SubRT constructor before call to aMakes an error= " +
    "\n SubRT constructor= " + this);
    public void subCallsBase1()
    try
    System.out.println("Calling the method baseMakeserror1");
    baseMakesError1(subNull);<---------------------This is where I am getting the error it says "ThomasR11.java [79:1] baseMakesError1(int) in ThomasR11.BaseRT cannot be applied to (ThomasR11.SubRT)"
    catch(NumberFormatException NFE)
    System.err.println("\nNumber Format Exception");
    catch (NullPointerException NPE)
    System.err.println(NPE.getMessage() + "\n");
    System.err.println(NPE.toString() + "\n");
    throw NPE;
    public void subError2()
    try
    BufferedReader in = new BufferedReader( new FileReader( "c:/xx/xx.xx" ) );
    catch(NullPointerException NPE)
    System.out.println("Null Pointer Exception");
    catch (FileNotFoundException FNFE)
    System.err.println("FileNotFoundException");
    public void subError3( String sX)
    try
    System.out.println("Hello");
    System.out.println (sX.length());
    catch(NullPointerException NPE)
    System.err.println("Null Pointer Exception");

    "ThomasR11.java [79:1] baseMakesError1(int) in ThomasR11.BaseRT cannot be applied to (ThomasR11.SubRT)"
    ...this error is telling out that you wrote baseMakesError1 to accept an "int" as an argument, and you are trying to pass it an instance of the ThomasR11.SubRT class. You either have to change baseMakesError1 to accept a SubRT as an argument, or change your call to pass an integer.

  • Calling a method from another servlet? very beginner

    I am going to try to explain what I want to do so just be patient please.
    I want to call a method in a seperate servlet to connect and release my connection pool - How do I do this?
    This is what I have been trying... help
    dbPOOL.class
    package DATABASE;
    import javax.naming.*;
    import javax.sql.*;
    import java.sql.*;
    import java.util.*;
    public class dbPOOL {
      Connection con;
      private boolean conFree = true;
      private String dbName = "java:comp/env/jdbc/connectDB";
      public dbPOOL() throws Exception {
           try  {              
                    InitialContext ic = new InitialContext();
                    DataSource ds = (DataSource) ic.lookup(dbName);
                    con =  ds.getConnection();    
         } catch (Exception ex) { throw new Exception("Couldn't open connection to database: " + ex.getMessage());
      public void remove () {
             try {
                 con.close();
            } catch (SQLException ex) { System.out.println(ex.getMessage());}
      protected synchronized Connection getConnection() {
             while (conFree == false) {
                     try {
                             wait();
                     } catch (InterruptedException e) {
                  conFree = false;
                  notify();
                  return con;
        protected synchronized void releaseConnection() {
            while (conFree == true) {
                     try {
                        wait();
                     } catch (InterruptedException e) {
                  conFree = true;
                  notify();
    }and my worker servlet connTest
    package DATABASE;
    import javax.naming.*;
    import javax.sql.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.sql.*;
    import java.util.*;
    public class connTest extends HttpServlet {  
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, SQLException, IOException {
             try {
                    String selectStatement = "select * " + "from mt.Evendale_Web_Groups";
                    getConnection();
                    PreparedStatement prepStmt = con.prepareStatement(selectStatement);
                    ResultSet rs = prepStmt.executeQuery();
                    while (rs.next()) {
                       groupList gl = new groupList(rs.getString(1), rs.getString(2), rs.getString(3));
                    prepStmt.close();
            } catch (SQLException ex) { throw Exception(ex.getMessage());}
                releaseConnection();
    }When I try to compile the connTest servlet - I keep getting cannot find symbols errors on the methods. How do I fix this?

    Which errors exactly?
    Try something like this:
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    dbPOOL db = null;     
    try {               
    String selectStatement = "select * " + "from mt.Evendale_Web_Groups";
    db = new dbPOOL();        
    Connection con = db.getConnection();               
    PreparedStatement prepStmt = con.prepareStatement(selectStatement);               
    ResultSet rs = prepStmt.executeQuery();               
    while (rs.next()) {                                    
       // process your rs                      
    prepStmt.close();           
    } catch (Exception ex) {
    } finally {
        if (db != null)
             db.releaseConnection();             
    }Simply change the package name to something else. But don't forget to import your dbPOOL class since it is now located in a diff package. Also be aware of coding standards when naming your classes and packages.

  • Compilation error while calling static method from another class

    Hi,
    I am new to Java Programming. I have written two class files Dummy1 and Dummy2.java in the same package Test.
    In Dummy1.java I have declared a static final variable and a static method as you can see it below.
    package Test;
    import java.io.*;
    public class Dummy1
    public static final int var1= 10;
    public static int varDisp(int var2)
    return(var1+var2);
    This is program is compiling fine.
    I have called the static method varDisp from the class Dummy2 and it is as follows
    package Test;
    import java.io.*;
    public class Dummy2
    public int var3=15;
    public int test=0;
    test+=Dummy1.varDisp(var3);
    and when i compile Dummy2.java, there is a compilation error <identifier > expected.
    Please help me in this program.

    public class Dummy2
    public int var3=15;
    public int test=0;
    test+=Dummy1.varDisp(var3);
    }test+=Dummy1.varDisplay(var3);
    must be in a method, it cannot just be out somewhere in the class!

Maybe you are looking for

  • The ITS service "bbpsc01" had to be terminated

    Hi All, While accessing any of shopping cart transaction bbpsc01/02/03 from web bowers it gives error as- *****************************web error************************************************ Note The following error text was processed in the system

  • Printing specific pages in a PDF

    What is the java code to create a print button to print specific pages in a multiple page PDF file? Help please!

  • Which Linux is the best for a X60 and T60/61?

    @Ajkula66, which version or flavour of Linux do you think is the best for a X60 and T60/61? Regards, Jin Li May this year, be the year of 'DO'! I am a volunteer, and not a paid staff of Lenovo or Microsoft Solved! Go to Solution.

  • How do I get my iTunes on my desktop working again?

    My desktop and laptop has can up saying it needed the update and about halfway through the upgrade it crashed. It was saying that there was an error coming up saying that iTunes wasn't installed on either. What happended? Does this mean that I have l

  • How to uninstall Xcode 4.3.3?

    The reason why is because I mean't to download it under another Apple id. I've tried the sudo command and all it says "command not found".