What are the different scopes for Java variables?

Discussion in 'Java' started by Software Engineer, Aug 1, 2011.

  1. What are the different scopes for Java variables?

    The scope of a Java variable is determined by the context in which the
    variable is declared. Thus a java variable can have one of the three
    scopes at any given point in time.
    1. Instance : - These are typical object level variables, they are
    initialized to default values at the time of creation of object, and
    remain accessible as long as the object accessible.
    2. Local : - These are the variables that are defined within a method.
    They remain accessbile only during the course of method excecution.
    When the method finishes execution, these variables fall out of scope.
    3. Static: - These are the class level variables. They are initialized
    when the class is loaded in JVM for the first time and remain there as
    long as the class remains loaded. They are not tied to any particular
    object instance.

    http://dev.fyicenter.com/
     
    Software Engineer, Aug 1, 2011
    #1
    1. Advertising

  2. Software Engineer

    markspace Guest

    On 7/31/2011 7:18 PM, Software Engineer wrote:
    > What are the different scopes for Java variables?


    <http://download.oracle.com/javase/tutorial/java/javaOO/variables.html>

    Block scope. Method parameters. Probably various and sundry nested
    classes too.
     
    markspace, Aug 1, 2011
    #2
    1. Advertising

  3. Software Engineer

    Lew Guest

    "Software Engineer" wrote:
    > What are the different scopes for Java variables?
    >
    > The scope of a Java variable is determined by the context in which the
    > variable is declared. Thus a java [sic] variable can have one of the three
    > scopes at any given point in time.
    > 1. Instance : - These are typical object level variables, they are
    > initialized to default values at the time of creation of object, and
    > remain accessible as long as the object accessible [sic].
    > 2. Local : - These are the variables that are defined within a method.
    > They remain accessbile only during the course of method excecution.
    > When the method finishes execution, these variables fall out of scope.
    > 3. Static: - These are the class level variables. They are initialized
    > when the class is loaded in JVM for the first time and remain there as


    That is not true. They are not initialized when the class is loaded, but later.

    Check your facts. It's in the JLS (Java Language Specification).

    > long as the class remains loaded. They are not tied to any particular
    > object instance.


    It's a good start, but incomplete and not entirely accurate. Check your facts and try again.

    markspace gave you a link to a (correct) tutorial and mentioned a couple of things you omitted.

    Yay, Brenden. He's a good programmer.

    --
    Lew
     
    Lew, Aug 2, 2011
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Armin Wagenknecht
    Replies:
    0
    Views:
    402
    Armin Wagenknecht
    May 4, 2004
  2. Miki Tebeka

    Question about nested scopes

    Miki Tebeka, Oct 8, 2003, in forum: Python
    Replies:
    3
    Views:
    307
    Miki Tebeka
    Oct 9, 2003
  3. Fernando Rodriguez
    Replies:
    2
    Views:
    293
    Alexander Schmolck
    Nov 21, 2003
  4. Dave Benjamin

    Nested scopes and class variables

    Dave Benjamin, Jan 31, 2005, in forum: Python
    Replies:
    7
    Views:
    407
    Steven Bethard
    Feb 3, 2005
  5. Yuh-Ruey Chen
    Replies:
    33
    Views:
    366
    David A. Black
    Nov 2, 2008
Loading...

Share This Page