Core Java Interview Questions and Answers
<< Previous Question Next Question >>
 Question: 1858 Page Views: 

What are the different scopes for Java variables?



Posted By: Avi Date: 30 October 2009 04:56:04 AM
 Answer:

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


Posted By: eTechPlanet


Date: 30 October 2009 04:56:04 AM

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.


Posted By: Johndecruse


Date: 10 February 2010 02:18:52 AM
Post a better Answer if you have
 
(Will show your Gravatar icon)  
  Country flag

Loading
Enter the text as shown in the image Pb9eEI
Related Questions
Core Java : What is a Java package and how is it used?

What is a Java package and how is it used?

A Java package is a naming context for classes and interfaces. A package is used to create a separat....
Category: Core Java Date: 11/4/2010 9:52:55 PM
Core Java : Why do threads block on I/O?

Why do threads block on I/O?

To other threads may execute while the i/o operation is performed
Category: Core Java Date: 11/4/2010 9:31:49 PM
Core Java : What is the Collections API?

What is the Collections API?

The Collections API is a set of classes and interfaces that support operations on collections of obj....
Category: Core Java Date: 11/4/2010 8:59:48 PM
Core Java : Is "abc" a primitive value?

Is "abc" a primitive value?

The String literal "abc" is not a primitive value. It is a String object.
Category: Core Java Date: 11/4/2010 8:47:53 PM
Core Java : What is the difference between List, Set and Map?

What is the difference between List, Set and Map?

A Set is a collection that has no duplicate elements. A List is a collection that has an order assoc....
Category: Core Java Date: 11/4/2010 8:15:52 PM