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)

Similar Messages

  • 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.

  • 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

  • A basic question about static variables and methods

    What would be the effects if I declare lots of static variables and static methods in my class?
    Would my program run faster?
    Would the class take more memory spaces? (If so, how do I estimate an appropriate number of variabls and methods to be declared static?)
    Thank you @_@

    when you declare a static var the var isn't created for every instance of the class, it just "live" in the class
    when you declare a static method is the same, so if you have:
    class MyClass
    static int myMethod()
    //Method work
    you dont need to have a instance of the class to call myMethod, you can do such things like this.
    int value = Myclass.myMethod();
    if myMethod isn't static you can't do this.. so, if
    class MyClass
    int myMethod()
    //Method work
    you CAN'T call
    int value = MyClass.myMethod();
    instead, you have to write
    MyClass m;
    m = new MyClass();
    value = m.myMethod();

  • About static variable initialization

    Here is an exercise where you have to guess the output :-)
    public class MyClass{
        private static int x = getValue();
        private static int y = 5;
        private static int getValue(){
            System.out.print("Running getValue ");
            return y;
        public static void main(String[] args){
            System.out.println(x);
    }This code outputs "Running getValue 0" I don't understand why?

    because class initialisation will call getValue() before it initialises 7 to 5, thus setting x to the value y has before being initialised which is 0.
    What this tells you is that you should never program rubbish like that, and in general not rely on the value of uninitialised members.

  • About  static variable

    anybody can tell why the answer is B,? i think it should be D.
    thanks
    1. public class test (
    2. private static int j = 0;
    3.
    4. private static boolean methodB(int k) (
    5. j += k;
    6. return true;
    6. )
    7.
    8. public static void methodA(int i) {
    9. boolean b:
    10. b = i < 10 | methodB (4);
    11. b = i < 10 || methodB (8);
    12. )
    13.
    14. public static void main (String args[] } (
    15. methodA (0);
    16. system.out.printIn(j);
    17. )
    18. )
    What is the result?
    A. The program prints �0�
    B. The program prints �4�
    C. The program prints �8�
    D. The program prints �12�
    E. The code does not complete.
    Answer: B

    Because the boolean OR (||) in java short-circuits.
    since the first part of this expression:
    b = i < 10 || methodB (8); is true, the second part (the call to methodB() is never excecuted.
    ~Tim
    (the boolean OR (|) is the non-short-circuiting version)

  • (JC) static variable and derived object

    Hi there!
    It is glad to know you from Java Card Forum. Can I ask for your help on the following question?
    It is about static variable. The following is my sample code:
    ========================================
    package com.Test01;
    import javacard.framework.*;
    import javacard.security.*;
    import javacardx.crypto.Cipher;
    public class Test01 extends Applet {
    OwnerPIN Pin;
    static DESKey[] keys;
    protected Test01(byte[] buffer, short offset, byte length) {
    keys = new DESKey[4];
    length = 0;
    while (length < 4) {
    keys[length] = (DESKey)KeyBuilder.buildKey((byte)3, (short)0x80, false);
    length = (byte)(length + 1);
    public static void install(byte buffer[], short offset, byte length) {
    new Test01(buffer, offset, length);
    ===========================================================
    If there are two instances, A and B, created in the package.
    My issues:
    1. Are keys[0]~ keys [3] kept while B is deleted?
    2. Does each instance have itsown object while "keys[length] = (DESKey)KeyBuilder.buildKey((byte)3, (short)0x80, false);"? or they share the same object?
    3. follow item 2, if A and B share the same object, is the object kept while B is deleted? Where can I get the information in Sun's spec?
    Thank you very much.
    Best regards,
    kantie

    Thanks a lot. You mean that keys variable will be removed while instance B deleted, right? I think the idea of database applet is very good, but I can't force applet provider the way of their implementations : )
    I still got question, does it get no use to set the keys variable to be "static"? So that I can keep it to other instances, if applied.
    And I think that any object derived under the static variable shall be kept, until the package is deleted.
    For example, if you declare a static pointer, and it points to an object newed by the instance at the first time. We say every instances (A and B) of this package (PckM) share this same static pointer and this same object, right? There are two situations:
    1. if this referred object is removed when B is deleted, so the memory of this object will be released. Then user might create an instace C of another package (PckN), and instance C new its objects just overlapping on the released memory. In this case, it will cause instance A to be crashed, because its static pointer has been referred to illeagle address.
    2. if this referred object is kept when B is deleted, then instance C will new its objects in other free memory. In this case, instance A will work well, because its static pointer still refers to correct object.
    What do you think? Am I missing any concepts?
    Thank you for your great opinions.
    Best regards,
    kantie

  • 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.

  • 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);
    }

  • How to go to java section global variable declaration

    Hi all
    i am following this scenario
    http://www.sdn.sap.com/irj/scn/weblogs;jsessionid=(J2EE3417100)ID0446700150DB10376299506581707969End?blog=/pub/wlg/11287
    in this scenario 3rd screen shot is about global variable declaration:
    In order to build the content of the attachment (for this particular requirement) we use a global variable declared and initialized in the global sections :
    can any body tell me how to go to this java section screen , as i am unable to find this java screen....
    Thanks
    sandeep

    even i cant see this edit java section
    i am working on PI 7.1. has this feature been removed in pi 7.1
    this is replaced in PI7.1 ... check this blog on how to use the "global" variable in PI7.1:
    /people/william.li/blog/2008/02/13/sap-pi-71-mapping-enhancements-series-using-graphical-variable
    Regards,
    Abhishek.

  • How to load value to a static variable on the run

    hi all
    i have a question about static variable. i need to have a variable to keep a value from DB shared by all instances. the variable is given value when the first instance is created. but from time to time, the value in DB may change, but i still need to maintain this shared value among instances. the static variable has life time as long as the program runs, does that mean if i need to change the value, i need to stop the program, and restart to load the new value? thanks.

    can the static variable be accessed within a
    non-static method, for instance, set the value by
    setXXX() method?Yes, and oddly enough, that usually how I access all my variables...
    Example...
    public class StaticTester {
           static String theString = " My Message ";
           public void setMessage(String mess){
                      theString = mess;
           public String getMessage(){
                      return theString;
           public static void main(String[] theArgs){
                      StaticTester myTest = new StaticTester();
                    System.out.println(myTest.getMessage());
                    myTest.setMessage(" a New Message ");
                 System.out.println(myTest.getMessage());
    }Hope this helps...
    - MaxxDmg...
    - ' He who never sleeps... '

  • 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."

  • Can't we declare a static variable inside a memberfunction of a class?

    Hi,
    class A{
    public void fun()
    static int i=10;
    can' we declare static variable in member function of class?
    Thanks,

    It is a common idiom in C and C++, but it is forbidden
    in Java because it adds hidden dependencies.
    The C way of writing a serial number generator:
    int generate() {
    static int n = 0;
    return n++;
    }Pure C has only global functions. So it needs inner
    static variables to help to hide the data. I've had
    lots of headaches trying to make C programs with inner
    static variables work correctly because they usually
    are hidden in cross-reference listings.
    The Java way:
    public static class SerialNumberGenerator() {
    private static int n = 0;
    public static int generate() {
    return n++;
    }The code above is as static as the C code given
    before, but it tries to be more explicit (no hidden
    variables).Hum... have you tried to compile your sample ?
    (And anyway, what the hell would a static class be used for ???)
    But perhaps you meant:
    public final class SerialNumberGenerator {
       private static int n = 0;
       public static int generate() {
          return n++;

  • How to clear the value of variable declared with STATICS keyword in FM?

    Hi All,
    I am using a standard CRM FM:CRM_CLA_RES_UPDATE_FM in that a STATICS variable is declared. In my code this STATICS varible value is populating. Iam to clear that Varible.
    How to do that.
    Thanks and Regards,
    Shabeer Ahmed,

    If you have 6.0 or up version then you will have a spiral button for your ABAP editor in you application toolbar. this is to find enhancements provided to you for standard programs. if you click on this you will get the enhancement points which you can identify and use accordingly to change/edit your STATICS variable content.
    if not, then you'll probably have to write to OSS , if you can open the code and do the required correction.

  • Declaring Static Variables

    Is it a bad way to declare somany Static Variables in a program?
    -Achyuth B

    ok..
    So if in a swings program if i am using only one frame through out the program, by adding and deleting components on it.
    Then i can declare that frame as Static, right.
    So that i can call the same frame(and not instance of it) from different class.
    And if i have to take the value entered in a JTextField from another class, Then that JTextField can also be declared as Static right.
    -Achyuth B

Maybe you are looking for