Using a static variable declared in an applet in another class

Hi guys,
I created an applet and i want to use one of the static variables declared in teh applet class in another class i have. however i get an error when i try to do that...
in my Return2 class i try to call the variable infoPanel (declared as a static JPanel in myApplet...myApplet is set up like so:
public class myApplet extends JApplet implements ActionListener, ListSelectionListener
here are some of the lines causing a problem in the Return2 class:
myApplet.infoPanel.removeAll();
myApplet.infoPanel.add(functionForm2.smgframeold);
myApplet.infoPanel.validate();
myApplet.infoPanel.repaint();
here are some of the errors i get
dummy/Return2.java [211:1] package myApplet does not exist
myApplet.infoPanel.removeAll();
^
dummy/Return2.java [212:1] package myApplet does not exist
myApplet.infoPanel.add(functionForm2.smgframeold);
^
dummy/Return2.java [213:1] package myApplet does not exist
myApplet.infoPanel.validate();
^
dummy/Return2.java [214:1] package myApplet does not exist
myApplet.infoPanel.repaint();
^
please help! thanks :)

I don't declare any packages though....i think it just doesn't recognize myApplet for some reason..
other errors i got compiling are:
dummy/Return2.java [82:1] cannot resolve symbol
symbol : variable myApplet
location: class Return2
updateDesc.setString(3, myApplet.staticName);
I Don't get why i'm getting this error cuase they worked fine when myApplet was a standalone application, not an applet.
myApplet is in the same folder as Return2 and it compiles properly.

Similar Messages

  • Static used on global variable declaration

    I have inherited a CVI project that contains a single .C file and a couple of .UIR files with their associated .H files.
    Many of the global variables have STATIC in their declaration - I believe this is due to the fact the original developer originally had them inside various functions and at some point changed strategy to make them global, leaving the STATIC keyword in place when copy/pasting.
    As I understand it the ONLY implication of STATIC on a global variable would be to keep its visibility only to the .C file in which it is declared. In this instance that is moot since there is only one .C file in the project.
    Is there anything about LabWindows/CVI which I might not be aware of that makes this use of STATIC cause something different than if STATIC were not used?
    Thanks for any input you might have since I'm new to CVI

    Roberto and menchar said it well:
    "static variables are declared at compile time and survive when their block of code terminates. If declared at function level, their value survives from one call of the function to another, so that they can be used over time to store permanent values locale to the function. If declared at module level, they are common to all functions in the module and are allocated outside the stack so that their values are not lost during program life. In every case they can be accessed and modified in values by functions in the program. If arrays, they cannot be dinamically changed in size."
    "The static keyword can be used with a function name also, meaning the function name isn't exported to the linker, and the function is in scope only to code within the module."

  • To use a static variable in another class,but report NullPointerException

    when TableMain is running,I run testRecord so that let TableMain add a occur informatin and
    happened time in a row in TableMain,but when I run testRecord,java report a NullPointerException and I dont know how to solve this problem,thanks for helping me to check my code;(error report info is in end)
    import java.awt.event.*;
    import javax.swing.table.*;
    import java.io.*;
    public class TableMain extends JFrame{
    JTable table;
    static OwnModel model;
    String[] columnHeader={"occur","time"};
    class OwnModel extends DefaultTableModel{
    public OwnModel(Object[] columnNames,int numRows){
    super(columnNames,numRows);
    public boolean isCellEditable(int row,int column){
    return false;
    public TableMain(){
    model=new OwnModel(columnHeader,0);
    table=new JTable(model);
    JScrollPane scroll=new JScrollPane(table);
    JButton save=new JButton("save record");
    JButton delete=new JButton("delete record");
    JPanel buttons=new JPanel();
    buttons.add(save);
    buttons.add(delete);
    JLabel sign=new JLabel("occur record");
    Container cp=getContentPane();
    cp.add(BorderLayout.NORTH,sign);
    cp.add(BorderLayout.CENTER,scroll);
    cp.add(BorderLayout.SOUTH,buttons);
    this.setSize(300,500);
    this.setVisible(true);
    this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    public static void main(String[] args){
    new TableMain();
    import java.util.Vector;
    import java.util.Calendar;
    import java.text.SimpleDateFormat;
    public class testRecord{
    public static void main(String[] args){
    SimpleDateFormat simpledf=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
    String tableName="friends";
    Calendar occurtime=Calendar.getInstance();
    Vector record=new Vector();
    record.add("send 'desc' sql, "+"operated table is :"+tableName);
    record.add(simpledf.format(occurtime.getTime()));
    System.out.println("model is "+Guide.model);
    System.out.println("record is "+record);
    Guide.model.addRow(record);
    error report:
    model is null
    record is [send 'desc' sql,operated table is :friends, 2004/05/26 11:39:03]
    Exception in thread "main" java.lang.NullPointerException
    at testRecord.main(testRecord.java:14)

    but I just use this constructor once,never use twice
    with same jvm;I thinks my idea is not too very badIt's a public constructor (if I'm looking in the right
    place) - it could be called from anywhere. To
    intialize a static variable outside the declaration,
    use a static initializer:private static String whatever;static {
    whatever = "whatever";
    }Or simply (in this case):private static String whatever = "whatever";

  • Static variable, a bug in applet classloader, Java plug-in?

    I found a potential bug in java plug-in.
    Environment,
    1. IE 6.0
    2. Windows XP
    3. JRE 1.3.1.06 and JRE 1.4.1.01
    Steps,
    1. build a very simple applet (attached below)
    2. embed the applet into a .html page.
    3. enable java plug-in for IE and Netscape.
    4. launch the .html page in either of the two browsers.
    5. for the 1st time, I can see 'tmp = 0' in the java console window.
    6. keep the browser open, browse to 'www.google.com'
    7. click 'Back' button of the browser.
    8. I notice 'tmp = 1' in the java console window.
    9. if I browse to google.com and back again, it will display 'tmp = 2'. and the number will keep on increasing if I repeat those steps.
    10. But if I press F5 to refresh the page, the log message will go back to 'tmp = 0'. and if I press 'x' inthe console window to clean up the classloader cache, I will get 'tmp = 0'. If I close the browser and open a new one, I will still get 'tmp = 0', which is what the applet should output.
    11. If I click 't' in the console window to list the thread while I am in the 'google' page, I can't see the applet thread, which means the applet should have been destroyed.
    12. No matter I try 'g' to do garbage collect, or 'f' to finalize objects, the problem still happens. the only solution is what I did in the step 10.
    Is it a bug in the classloader or the JRE itself? Why the value of the static variable won't be cleaned up along with the applet?
    ----------- HERE IS THE APPLET --------------------
    import java.applet.Applet;
    public class AA extends Applet
    private static long tmp = 0;
    public void init() {
         System.out.println("tmp value = " + tmp);
    tmp++;
    public void start() {}
    public void stop() {}
    public void destroy() {}

    Static values are stored in the class. Once a class is loaded into a classloader, it cannot be unloaded (the only thing you can do is use another classloader).
    Apparently, the classloader cache allows sharing of classloaders between applet instances. Perhaps F5 (reload) causes the classloader cache to be cleared.
    I don't see any bug. When developing applications or applets that must run in the same JVM, you should program defensively and avoid using static values which can change over time.

  • Static variable declaration in JavaFX

    Hello, I'd like to ask how to substitute static key word to declare a variable, that is visible within the current package and accessible without creating an instance of a class, that contains this variable. Because my NetBeans insists that static variable is depricated.
    Thank you.

    In Foo.fx...
    class Foo {
        // bar is an instance variable that has public access
        public def bar = "bar";
    // baz is a "static" variable that has public access
    public def baz = "baz";
    // someFun is a "static" function
    public function someFun() : Void {
    }In Main.fx...
    println("{Foo.bar}"); // compile error - cannot reference bar from static context
    println("{Foo.baz}"); // This is ok - Foo.baz is static
    var foo = Foo{}
    println("{foo.bar}"); // This is ok. bar referenced from Foo instance

  • Clarification about static variables declaration

    I'm getting "Illegal Start of Expression" error, When i try to declare a static variable within a static metho or nonstatic method.
    Could you please clarify me,
    Is it possible to declare a static varible within a static method or non static method ?
    Thanks

    Hi Vikas,
    First, note that this forum is devoted to Sun Java Studio Creator IDE. General Java questions can be asked on forums here: http://forum.java.sun.com/
    Second, note that static variable can be defined only as member of class. It cannot be defined inside of method.
    Thanks, Misha
    (Creator team)

  • Updating a frame / use of static variables

    Hello,
    I am working with a program (installer) that walks the user through a series of question/response frames. My first question has to do with refreshing the frame with the new contents whenever an event is triggered by a button. I have a method that works, but I am wondering if it is the best way of doing this. I have done much searching on this, but only seem to find instruction on using revalidate/repaint(), which I understand to be for graphics, or examples for things like changing the text on a button using setText().
    I actually don't have a problem with doing it the way that I am, but wonder,
    1) is this the most elegant (or correct) way,
    2) is this violating the philosophy of encapsulation since I have to make
    the panels and frame static fields to make this work, because (as
    far as I can tell) there is no way to pass parameters using event listeners.
    3) am I just being really paranoid about the encapsulation thing?
    Thanks for your input, here is a stripped down example of my code:
    Allasso
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class FrameDemo extends JFrame
        implements ActionListener {
        JLabel label1;
        JLabel label2;
        JButton button1;
        JButton button2;
        static JPanel panel1;
        static JPanel panel2;
        static FrameDemo frame = new FrameDemo();
        FrameDemo(){};
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("quit")) {
                System.exit(0);
            if (e.getActionCommand().equals("openFrameTwo")) {
                frame.createFrame2();
        //code to set the frame size and location, etc HERE....
        //frame creating methods...
        public void createFrame1(){
            panel1 = new JPanel();
            panel2 = new JPanel();
            label1 = new JLabel("Message for frame ONE");
            panel1.add(label1);
            button1 = new JButton("open frame two");
            button1.setActionCommand("openFrameTwo");
            button1.addActionListener(this);
            button2 = new JButton("quit");
            button2.setActionCommand("quit");
            button2.addActionListener(this);
            panel2.add(button1);
            panel2.add(button2);
            frame.getContentPane().add(panel1,BorderLayout.NORTH);
            frame.getContentPane().add(panel2,BorderLayout.CENTER);
            frame.pack();  //actually not used in my program; just in this demo
            frame.setVisible(true);
        //Button on frame 1 pressed, action event calls createFrame2()...
        public void createFrame2(){
            frame.getContentPane().remove(panel1);      // this is the code I am mainly talking about,
            frame.getContentPane().remove(panel2);      // used to "refresh" the frame.
            panel1 = new JPanel();
            panel2 = new JPanel();
            label1 = new JLabel("Message for frame TWO");
            panel1.add(label1);
            button2 = new JButton("quit");
            button2.setActionCommand("quit");
            button2.addActionListener(this);
            panel2.add(button2);
            frame.getContentPane().add(panel1,BorderLayout.NORTH);
            frame.getContentPane().add(panel2,BorderLayout.CENTER);
            frame.pack();  //actually not used in my program; just in this demo
            frame.setVisible(true);
        public static void main(String s[]) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    FrameDemo fd = new FrameDemo();
                    //fd.codeToSetThingsUp();
                    fd.createFrame1();      //display the first frame, and start the daisy chain...
    }Edited by: Allasso on Feb 19, 2010 7:32 AM

    Joerg22 wrote:
    I don't get two independent frames, I only get one at a time.Correct. I got fooled by the method name.
    Yeah, sorry about that...
    But my basic statement is not shaken: You create and destroy several panels, whereas one panel would be sufficient.Yes, I am starting to see what you mean. See below:
    Your additional actions (JFileChooser, JTextArea) are no hindrance for using a card layout, since I assume you will operate always from a specific panel.
    Okay.
    Still, it may turn out that you are most happy with your original design, for I assume the creation and destruction of multiple panels will not become a performance issue in your case.
    This is largely an academic experience (even though I intend to use the program I am working on). Basically, I am exploring Java, and also trying to get a feel for what means are congruent with the spirit of OO programming. So if something is a performance issue, or any other issue, I am interested in knowing about it.
    This is my first experience with GUI in any language, and essentially I am new to OO design as well. All of my previous experience has been with scripting.
    Bye
    JörgYour input is much appreciated, thanks.
    Allasso
    And now, this is what I have found so far, in regard to (not) creating and destroying panels. One note here as well, is that I found that if I placed my instructions for changing the panels (whether it be destroy/create or just changing the contents thereof) directly in the actionPerformed() method, rather than making them each separate methods and calling those methods from the actionPerformed() method, I did not have to declare the panel variables as static. Also, I declare the frame upon creation of it, rather than making it a field. So the result is I have eliminated all static fields.
    For academic sake, however, I still do not understand why using frame.removeAll() behaves the way it does (see reply 2 above).
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    public class FrameDemo extends JFrame
        implements ActionListener {
        JLabel label1;
        JLabel label2;
        JButton button1;
        JButton button2;
        JPanel panel1;
        JPanel panel2;
        FrameDemo(){};
        public void actionPerformed(ActionEvent e) {
            if (e.getActionCommand().equals("quit")) {
                System.exit(0);
            //Button on frame 1 pressed, action event calls frameDisplay2()...
            if (e.getActionCommand().equals("openFrameTwo")) {
                panel1.removeAll();
                panel2.removeAll();
                label1 = new JLabel("Message for frame TWO");
                panel1.add(label1);
                panel1.revalidate();
                panel1.repaint();
                button2 = new JButton("quit");
                button2.setActionCommand("quit");
                button2.addActionListener(this);
                panel2.add(button2);
                panel2.revalidate();
                panel2.repaint();
        //code to set the frame size and location, etc HERE....
        //frame creating methods...
        public void frameDisplay1(){
            FrameDemo frame = new FrameDemo();
            panel1 = new JPanel();
            panel2 = new JPanel();
            label1 = new JLabel("Message for frame ONE");
            panel1.add(label1);
            button1 = new JButton("open frame two");
            button1.setActionCommand("openFrameTwo");
            button1.addActionListener(this);
            button2 = new JButton("quit");
            button2.setActionCommand("quit");
            button2.addActionListener(this);
            panel2.add(button1);
            panel2.add(button2);
            frame.getContentPane().add(panel1,BorderLayout.NORTH);
            frame.getContentPane().add(panel2,BorderLayout.CENTER);
            frame.pack();  //actually not used in my program; just in this demo
              frame.setLocationRelativeTo( null );
            frame.setVisible(true);
        public static void main(String s[]) {
            javax.swing.SwingUtilities.invokeLater(new Runnable() {
                public void run() {
                    FrameDemo fd = new FrameDemo();
                    //fd.codeToSetThingsUp();
                    fd.frameDisplay1();      //display the first frame, and start the daisy chain...
    }

  • Best Practice:  Using a static variables and methods vs singleton pattern

    Just curious, since when anything, within a class, is denoted as static it typically will be stored in main memory as a class property(method, variables, etc). Is it a good practice to be doing this all the time or is the singleton pattern a better choice. Please explain why. Thanks.

    I wouldnot make anything other than the constants and the instance itself static. And I cant think of a reason wjy one would.

  • I want to use static variable instead of using variable in servlet context

    Hi all,
    In my web application i have to generate a unique Id.
    For this, At the application startup time i am connecting to the database and getting the Id and placing it in the servlet context.
    Every time i am incrementing this id to generate a unique id.
    But, now i want to place this id in a static variable which is available to all the classes.
    why i want to do this is to reduce burden on servlet context.
    my questing is, is this a best practice ? If not please give me your valuable suggestion.
    thanks
    tiru

    There isn't a problem with this as long as you want to share the value of that variable with all requests. If this is read-only except when it is first set then you're fine. The only real issue will be how to initialize and/or reinitialize the variable. When the servlet is started, how will you get the value for the variable? If the servlet is shutdown and restarted (a possibility in any application server) how will you re-read the variable? You need to answer these questions to decide the best route. It may be as simple as a static initializer or it may be more complex like a synchronized method that is called to see if the variable is set.

  • Non-static variable from a static context

    This is the error i get . If i understand the error correctly it says im using a static variable when i shouldnt be? Or is it the other way round? below the error is the actual code....
    The error...
    Googler.java:27: non-static variable this cannot be referenced from a static context
              submitButton.addActionListener(new ButtonHandler());The code...
              JButton submitButton = new JButton("Submit Query");
              submitButton.addActionListener(new ButtonHandler());

    thanks for the response.
    I have already tried what you said but I tried it again anyway and i get the same error more less...
    Googler.java:28: non-static variable this cannot be referenced from a static context
              ButtonHandler buttonHandler = new ButtonHandler();here is part of my code
    public class Googler
      static JTextField input1, input2;
         public static void main(String[] args)
              JFrame myFrame = new JFrame("Googler v1.0");
              Container c = myFrame.getContentPane();
              JLabel lab1 = new JLabel("Enter Google Query:");
              JLabel lab2 = new JLabel("Enter Unique API Key:");
              input1 = new JTextField(15);
              input2 = new JTextField(15);
              JRadioButton radSearch = new JRadioButton("Search Query");
              JRadioButton radCached = new JRadioButton("Cached Query");
              JButton submitButton = new JButton("Submit Query");
              ButtonHandler buttonHandler = new ButtonHandler();
              submitButton.addActionListener(buttonHandler);
              ButtonGroup group = new ButtonGroup();
              group.add(radSearch);
              group.add(radCached);Ive tried declaring buttonHandler as a static variable and this dosn't work either. I've never had this problem before it must be something silly im missing...?
    Thanks
    Lee

  • Runtime error in linking with static variables....

    Hi,
    I am building a shared library which includes a compiled object generated from a class containing the static variables only and also I have another version of same class with same & some new static variables in it and using that to generate another shared library. Now when I tried to run a program
    which links with both the library it core dumps when it tries to return from the program i.e when it finishes.
    Can someone please help me explain why my program is behaving like that?? Can duplicate inculsion of static variables in different scope can cause problem ?? How can this be avoided ?
    Also please be advised that the class with static variables gets generated at the compile time using a script which uses a DTD whose version (specification) can be different in both the libraries therefore I can't just seperate the common stuff into one class and specific into another.
    Thanks.
    Rajeev.

    Not to worry...found the answer in another post. Seems like patches need to applied to the OS.

  • Non-static variable cant accessed from the static context..your suggestion

    Once again stuck in my own thinking, As per my knowledge there is a general error in java.
    i.e. 'Non-static variable cant accessed from static context....'
    Now the thing is that, When we are declaring any variables(non-static) and trying to access it within the same method, Its working perfectly fine.
    i.e.
    public class trial{
    ���������� public static void main(String ar[]){      ////static context
    ������������ int counter=0; ///Non static variable
    ������������ for(;counter<10;) {
    �������������� counter++;
    �������������� System.out.println("Value of counter = " + counter) ; ///working fine
    �������������� }
    ���������� }
    Now the question is that if we are trying to declare a variable out-side the method (Non-static) , Then we defenately face the error' Non-static varialble can't accessed from the static context', BUT here within the static context we declared the non-static variable and accessed it perfectly.
    Please give your valuable suggestions.
    Thanks,
    Jeff

    Once again stuck in my own thinking, As per my
    knowledge there is a general error in java.
    i.e. 'Non-static variable cant accessed from static
    context....'
    Now the thing is that, When we are declaring any
    variables(non-static) and trying to access it within
    the same method, Its working perfectly fine.
    i.e.
    public class trial{
    ���������� public static void
    main(String ar[]){      ////static context
    ������������ int counter=0; ///Non
    static variable
    ������������ for(;counter<10;) {
    �������������� counter++;
    ��������������
    System.out.println("Value
    of counter = " + counter) ; ///working fine
    �������������� }
    ���������� }
    w the question is that if we are trying to declare a
    variable out-side the method (Non-static) , Then we
    defenately face the error' Non-static varialble can't
    accessed from the static context', BUT here within
    the static context we declared the non-static
    variable and accessed it perfectly.
    Please give your valuable suggestions.
    Thanks,
    JeffHi,
    You are declaring a variable inside a static method,
    that means you are opening a static scope... i.e. static block internally...
    whatever the variable you declare inside a static block... will be static by default, even if you didn't add static while declaring...
    But if you put ... it will be considered as redundant by compiler.
    More over, static context does not get "this" pointer...
    that's the reason we refer to any non-static variables declared outside of any methods... by creating an object... this gives "this" pointer to static method controller.

  • Best use of  static constants in project level

    Generally , we use magic numbers in loops & conditional statements in our code. But we have a better option of using final static variables(constants) instead of magic numbers. But the problem is if we need to use these constants in whole project then what is the best suitable place where we can put these constants in project.

    At some part of the project everyone uses anyway.

  • Static variable doubt !!!

    why the following code throws an error (Illegal start of expression )
    public class samp
         public static void main(String [] a)
    static int i=10; // error is occuring at this line ....
         System.out.println(i);
    }

    In C you can have a static variable declared inside a function.
    It is static, that is, retains the value between the calls, and it is declared in the function, hence it is seen only from there. In the following example both function have their own static i variables.
    #include <stdio.h>
    static int i=0;
    int foo() {
    static int i=100;
         return i++;
    int bar() {
    static int i=200;
         return i++;
    int main() {
    int x;
    for(x=4;x<8;x++) printf("%d %d\n",x,foo());
    for(x=4;x<8;x++) printf("%d %d\n",x,bar());
    printf("%d\n",i);
    }

  • Equivalent of visual Basic static variable in LabVIEW

    Hi all!
    I am doing a project where I am counting the values. After i Close/Stop the program I want the value to be intact and continue with the last value.
    I know this in Visual Basic. By using the Static variable we can retain the value even we close the program.
    Kindly help..
    Srikanth Chilivery,
    Solved!
    Go to Solution.

    I have trouble believing that a static variable retains its value after the program is terminated.  Once the program is released from memory, that static variable is gone.  If you want to retain a value between closing and opening an application, you have to save it to disk in some way.
    Are you working exclusively in the LabVIEW IDE or are you running EXEs?  It sounds like you probably just want to use a Functional Global Variable/Action Engine.
    There are only two ways to tell somebody thanks: Kudos and Marked Solutions
    Unofficial Forum Rules and Guidelines

Maybe you are looking for

  • How to sync Safari bookmarks to my iPad2?

    I've had my iPad2 for a few months now, and I noticed that my iMac Safari bookmarks are more current than those on my iPad2, even though I think I'm set up for them to be kept current using iCloud.  I use the cable to sync with iTunes on my iMac, and

  • (RXGLER) ERROR WHILE RUNNING REPORT ! URGENT !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!1

    HI, I AM RUNNING A REPORT WHICH IS BASED ON A TEMPORARY TABLE, THE DATA IN THIS TABLE IS COMMING FROM LOT OF OTHER TABLES AT RUNTIME THE MOMENT I CLICK ON RUN BUTTON IT SAYS : REP-3301:ERROR OCCURED IN ERROR CHECKING (RXGLER) AND MY NODE GETS HANGED

  • Upgrading to Leopard from Tiger. Should I clean install, or is archiving ok

    I have the capability to backup up everything myself and clean install, but my only issue is... I'm running a couple of programs that could be...might be....cracked versions. And I'm also running my own copy of the CS3 Adobe Master Collection, which

  • Upgrading my MAC OSX 10.6 to 10.6.2 or erlier

    Hi, I have a MACBOOK with the following configuration. Model Name: MacBook Model Identifier: MacBook5,1 Processor Name: Intel Core 2 Duo Processor Speed: 2.4 GHz Number Of Processors: 1 Total Number Of Cores: 2 L2 Cache: 3 MB Memory: 2 GB Bus Speed:

  • Purchase order customer data enhancements

    my requriment is custom tab add in purchase order in item level i using  Badi. i find the badi but in ME_GUI_PO_CUST Implemented but tab is not display, why? in this badi i m implemented all methods also. then other badi is there ME_PROCESS_PO_CUST