What are the different scopes for Java variables?

  • Thread starter Software Engineer
  • Start date
S

Software Engineer

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/
 
L

Lew

Software Engineer said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top