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.

Similar Messages

  • Problems with static member variables WAS: Why is the static initializer instantiating my class?!

    Hi,
    I have been hunting down a NullPointerException for half a day to come to
    the following conclusion.
    My constructor calls a method which uses static variables. Since an intance
    of my class is created in the static block when the class is loaded, those
    statics are probably not fully initialized yet and the constructor called
    from the static block has those null pointer problems.
    I've considered moving the initialization of the static variables from the
    declaration to the static block. But your code is inserted BEFORE any other
    code. Therefore not solving my problem.
    Two questions:
    1) what would be a solution to my problem? How can I make sure my static
    variables are initialized before the enhancer generated code in the static
    block calls my constructor? Short of decompiling, changing the code and
    recompiling.
    2) Why is the enhancing code inserted at the beginning of the static block
    and not at the end? The enhancements would be more transparent that way if
    the static variables are initialized in the static block.
    Thanks,
    Eric

    Hi Eric,
    JDO calls the no-args constructor. Your application should regard this constructor as belonging
    primarily to JDO. For example, you would not want to initialize persistent fields to nondefault
    values since that effort is wasted by JDO's later initilization to persistent values. Typically all
    you want to initialize in the no-args constructor are the transactional and unmanaged fields. This
    rule means that you need initialization after construction if your application uses the no-args
    constructor and wants persistent fields initialized. On the other hand, if your application really
    uses constructors with arguments, and you're initializing persistent fields in the no-args
    constructor either unintentionally through field initializers or intentionally as a matter of
    consistency, you will find treating the no-args constructor differently helpful.
    On the other hand, if Kodo puts its static initializer code first as you report, then it is a bug.
    Spec Section 20.16: "The generated static initialization code is placed after any user-defined
    static initialization code."
    David Ezzio
    Eric Borremans wrote:
    >
    Hi,
    I have been hunting down a NullPointerException for half a day to come to
    the following conclusion.
    My constructor calls a method which uses static variables. Since an intance
    of my class is created in the static block when the class is loaded, those
    statics are probably not fully initialized yet and the constructor called
    from the static block has those null pointer problems.
    I've considered moving the initialization of the static variables from the
    declaration to the static block. But your code is inserted BEFORE any other
    code. Therefore not solving my problem.
    Two questions:
    1) what would be a solution to my problem? How can I make sure my static
    variables are initialized before the enhancer generated code in the static
    block calls my constructor? Short of decompiling, changing the code and
    recompiling.
    2) Why is the enhancing code inserted at the beginning of the static block
    and not at the end? The enhancements would be more transparent that way if
    the static variables are initialized in the static block.
    Thanks,
    Eric

  • Non-static cannot be referenced from a static context - ?

    Hi, i understand (kinda) what the error means
    i think its saying that i cannot call a non-static method "bubbleSort"
    from the static method main (correct?)
    but i dont know how to fix it...
    do i make bubbleSort
    public static void bubbleSort???
    C:\jLotto\dataFile\SortNumbers.java:80: non-static method bubbleSort(int[]) cannot be referenced from a static context
              bubbleSort( a ); //sort the array into ascending numbers
    my code:
    import java.io.*;
    import java.util.*;
    public class SortNumbers
         public static void main( String args[]) throws IOException
          int num;
              int a[] = new int[7]; //an array for sorting numbers
              File inputFile = new File("C:\\jLotto\\dataFile\\outagain.txt");
              File outputFile = new File("C:\\jLotto\\dataFile\\sortedNum.txt");
            BufferedReader br = new BufferedReader( new FileReader( inputFile ));
          PrintWriter pw = new PrintWriter( new FileWriter( outputFile ));
          String line = br.readLine();
          while( line != null ){//reads a single line from the file
             StringBuffer buffer = new StringBuffer(31);          //create a buffer
             StringTokenizer st = new StringTokenizer( line," "); //create a tokenizer
             while (st.hasMoreTokens()){
                        // the first 4 tokens are id,month,day,ccyy, no sorting needed
                        // so they are simply moved into the buffer
                        for (int i =1; i<5; i++){
                             num = Integer.parseInt(st.nextToken());
                             buffer.append( num );
                             buffer.append( "|");
                        //tokens 5 to 11 need to be sorted into acending order
                        //so read tokens 5 to 11 into an array for sorting
                        for (int i =0; i<7; i++){
                             a[i] = Integer.parseInt(st.nextToken());
                     bubbleSort( a ); //sort the array into ascending numbers
                      //the array is sorted so read array back into the buffer
                      for ( int i = 0; i < a.length; i++ ){
                           buffer.append( a[ i ] );
                           buffer.append(  "|" ) ;
                   }//end of while st.hasMoreTokens
             //then write out the record from the stringBuffer
             pw.println( buffer );
             line = br.readLine();
          }//end of while != null
              br.close();
              pw.close();
         }//end of static main
       // sort the elements of an array with bubble sort
       public void bubbleSort( int b[] )
          int swapMade = 0; //if after one pass, no swaps were made - exit
          for ( int pass = 1; pass < ( b.length - pass) ; pass++ ) // passes reduced for speed
              for ( int i = 0; i < b.length - 1; i++ ) // one pass
                  if ( b[ i ] > b[ i + 1 ] )            // one comparison
                      swap( b, i, i + 1 );              // one swap
                      swapMade = 1;
                  }  // end of if
               } //end of one pass
             if (swapMade == 0) pass = 7; //no swaps, so break out of outter for loop
          }//end of passes, end of outter for loop
       }//end of bubblesort method
       // swap two elements of an array
       public void swap( int c[], int first, int second )
          int hold;  // temporary holding area for swap
          hold = c[ first ];
          c[ first ] = c[ second ];
          c[ second ] = hold;
       }   //end of swap
    }//end of class SortNumbers

    Static means
    when u run the program (a class), there is only one variabel / type in memory.
    ex static int a; //assume it's inside the aStaticClass
    mean no wonder how many u create object from this class, variabel a only have 1 in memory. so if u change a (ex a=1;) all instance of this aStaticClass will effected (because they share the same variabel).
    Try to read more at :
    http://java.sun.com/docs/books/tutorial/java/javaOO/classvars.html
    I hope this will help you....happy new year
    yonscun

  • How do I setup my Time Capsule (3rd Generation) to be accessed from the internet while I'm traveling?

    How do I setup my Time Capsule (3rd Generation) to be accessed from the internet while I'm traveling? It is installed on my home network behind my TWC broadband router.

    Ok.. since the TWC modem is also a router.. all configuration takes place on this box.. NONE whatsoever takes place on the TC.
    There is no airport utility 7.7.3 but there is a firmware of that number for the latest AC model TC..
    Is it tall like this.
    Then it is Gen5.
    otherwise it will have a firmware.. 7.6.4 or earlier and the airport utility must be 6.3 or earlier.
    Open the Airport utility and give us a screenshot of the summary page.
    That will also help us determine that you have the TC, which version and how it is setup.
    You might want to press the edit and also give us the Internet and Network tab as they should be set correctly as well.
    I have created a DDNS through DYN.com although I am not sure how to implement this into the TC.
    You do not do anything in the TC.. set it up in the Ubee router.
    Port forward 548 to the TC in the Ubee router.
    And make sure the TC has a static IP in the Ubee router.
    Overall if you find this too hard I strongly recommend you buy a product designed for remote access .. eg WD MyCloud.. they are cheap and easy peasy to setup for remote access.. by PC or Mac and since it is built outside of Apple you not bound up in Apple limitations built into all their equipment to prevent you using it the way you want.. rather than apple want you too.. eg BTMM and iCloud being the only way apple provide for access to the TC and only when it is the main router of the network.
    You are fighting hard because Apple made this hard.. not easy.

  • Cant access to the integrated web browser RG60SE

    Hi. I recently bought a RG60SE router. Following the instructions in the quick manual i tried to configure it, but i cant access to the integrated web based configuration utility. I tried access from three diferent pcs with same results. I tried to access connecting only the router and one PC, without ADSL modem, but not results.
    I tried to reset, plug, unplug, power off, power on, the PC and the router, in many combinations, but i have zero results.
    Im very frustrated, may be im doing something wrong?
    I have a one desktop directly connected to ADSL modem:
    IP       192.168.1.2
    Mask   255.255.255.248
    Gate   192.168.1.1
    DNS1  200.105.128.40
    DNS2  200.105.128.41
    Please, i need help,
    Thanks

    Thanks for your answer:
    Yes i am using http://192.168.1.1
    I have an another router D-link dir-400. When this router is resetted, i can see a d-link wireless signal within my notebook. But i can not see anything about MSI router.

  • How do I turn off the Camera access from the lock screen

    how do I turn off the Camera access from the lock screen ?

    Only by hiding the camera via Restrictions. Settings>General>Restrictions. That hides the camera when unlocked unless you go to Restrictions  and unset that Restriction. That is not way currently from just preventing camera access only from the lock screen

  • Why can I not adjust the volume on a video when accessed from the lock screen

    Why can I not adjust the volume of a video in my pictures folder, when accessed from the new lock screen option?

    There is no way to access videos directly from the lock screen.
    What exactly is being attempted here?

  • After downloading os5.0 ,none of my contacts reloaded from the backup. Any idea's how to get them back.

    After downloading OS 5.0, none of my contacts reloaded from the backup. Any idea how to get them back?

    I am having the same problem.....13 years of my HUSBAND'S business contacts are gone.  Let me know if you figure this out!

  • Hi, I need the program of access from the office, so i'm going to but the Microsoft office 365 university, will i have to but any other program to use the access from my mac? or to open any database that I will have did from any other computer (not mac)?

    hi, I need the program of access from the office, so i'm going to but the Microsoft office 365 university, will i have to but any other program to use the access from my mac? or to open any database that I will have did from any other computer (not mac)?

    There has never been a Mac version of Access. Microsoft never created one, realising that it was very inferior to the FileMaker Pro database application.
    So if you must run Access you will have to run Windows on your mac.

  • Outlook Web Access is currently unavailable. If the problem continues, contact technical support for your organization and tell them the following: No Client Access servers of the appropriate version can be accessed from the Internet

    Good Morning,
         We are getting this error 
    Outlook Web Access is currently unavailable. If the problem continues, contact technical support for your organization and tell them the following: No Client Access servers
    of the appropriate version can be accessed from the Internet
    We installed a new Exchange 2007 CAS on Windows 2008R2. Got rid of old CAS on Exchange 2007. Now seeing this error. Does anyone have an idea??

    Hi,
    If the issue persists, I recommend you install Exchange 2007 SP3 RU7 and check the result. Also, ensure that Exchange 2010 SP2 RU1 or later version is installed. Old Exchange version may lead to the CAS-to-CAS proxy incompatibility.
    What's more, here are some helpful blogs for your reference.
    Exchange 2010 SP2 RU1 and CAS-to-CAS Proxy Incompatibility
    http://blogs.technet.com/b/exchange/archive/2012/02/17/exchange-2010-sp2-ru1-and-cas-to-cas-proxy-incompatibility.aspx
    OWA Coexistence With Legacy Versions
    http://blogs.technet.com/b/sjimmie/archive/2010/07/09/owa-coexistence-with-legacy-versions.aspx
    Hope this can be helpful to you.
    Best regards,
    Amy Wang
    TechNet Community Support

  • IDVD 7.1.2: I am making music instrument instructional DVDs, and I want to include several audio loops in that the viewer access from the main menu to play along with

    iDVD 7.1.2: I am making music instrument instructional DVDs, and I want to include several audio loops that the viewer access from the main menu to play along with

    Export the slideshow out of iMovie via the File  ➙  Share ➙ File menu option as a 480p Quicktime movie.
    Open iDVD, select a theme and drag the exported QT movie file into the open iDVD window being careful to avoid any drop zones.
    Follow this workflow to help assure the best quality video DVD:
    Once you have the project as you want it save it as a disk image via the File ➙ Save as Disk Image  menu option. This will separate the encoding process from the burn process. 
    To check the encoding mount the disk image, launch DVD Player and play it.  If it plays OK with DVD Player the encoding is good.
    Then burn to disk with Disk Utility or Toast at the slowest speed available (2x-4x) to assure the best burn quality.  Always use top quality media:  Verbatim, Maxell or Taiyo Yuden DVD-R are the most recommended in these forums.
    OT

  • How to remove app designer migration access from the Developer id

    Team,
    Please guide me with steps to remove app designer migration access from the Developer id.
    I want that developer can create the project but do not have migration access.
    Rgds,

    Did you already remove the other permission lists/roles you identified in your previous response like I told you to do in my first response?  The user can have no permission lists that grant this access.  Working with a clone of VP1 is a bad strategy.  Security should be handled from the bottom up, adding what is required for a user to perform their necessary functions.  Cloning VP1 and removing roles/permissions is security from the top down and will undoubtedly leave you with a user that is still granted more access than you like even after removing this particular access.  You should really be working with your companies security admin on this, they may have a standard way of doing this.  Many places I've been use custom roles and permission lists when deviating from the Oracle delivered security.
    Does ABC user still have any access to UPGRADE?
    select ru.roleuser, ru.rolename, rc.classid, ai.barname from psroleuser ru
    join psroleclass rc on ru.rolename = rc.rolename
    join psauthitem ai on ai.classid = rc.classid
    where ai.menuname = 'APPLICATION_DESIGNER'
    and ai.barname = 'UPGRADE'
    and ru.roleuser = 'ABC'

  • I have a macbook and after i've done the upgrade to lion os i cant eject the cd from the tray! any suggestions???

    I have a macbook and after i've done the upgrade to lion os i cant eject the cd from the tray! any suggestions???

    Restart the computer (e.g. via the Apple menu). At the point you hear the startup chime (or sooner), hold down the trackpad or mouse button, and keep holding it down until after the point the Apple logo appears during startup.
    The CD should be ejected by the time the Apple logo appears.

  • HT4009 Hi i cant buy from the game clash of clans

    Hi i cant buy from the game clash of clans they told me to contact apple support please help

    Click here and request assistance.
    (84069)

  • HT1473 i just updated iTunes to 11.0, but now, for some reason, i cant songs from the computer to the library, why ?

    i just updated iTunes to 11.0, but now, for some reason, i cant songs from the computer to the iTunes library. Why ?

    SOLVED:
    refer to this forum if in need: https://discussions.apple.com/message/24603547#24603547

Maybe you are looking for

  • Impossible to access with hp SimplePass

    Hello! I recently formatted my hp dv6-6159sl (mintaining the same operating system Windows 8.1) and since than I no longer access Windows with fingerprint reader. In the logon screen Windows, as access option, shows only the password but not the fing

  • Multi-entity application

    Hi there, I'm developing a multi-entity application and I'm not sure how to optimize the data base. Let say big tables for each entity will have 15000 rows per year and we are expecting at least 150 entities, so we are talking about 2250000 rows per

  • Timecode concern

    I have a timecode concern. I have been logging recently developed footage that has been transfered to miniDV (6 tapes in all). It seems that all of tape 4 and half of tape 5 are without timecode. The "capture now" option is out considering that FCP "

  • Per folder umask level??

    Hi, I was wondering if it is possible to set the NSUmask level or something similar on a per folder basis?? I have a fairly restrictive set of permissions on my system, but would like to make the Sites folder default to 755 or 754 or something. Is it

  • IOS 7.0.6 for iPhone 4S

    Recently upgraded to 7.0.6 for iPhone 4S. Bad move but no choice. Now when I zoom into my photos it disappears, only to reappear when back to camera roll. Phone freezes at times, wifi also seems slow. Some cool new features but overall not liking som