GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.tutorialspoint.com — Java - Variable Scopes
Selected Reading
Java - Variable ScopesThe variable's scope refers to the region where they are created and accessed in a given program or function. The variable scope also refers to its lifetime. In this tutorial, we will learn about the scopes of the different types of Java variables. Scope of Java Instance VariablesA variable that is declared inside a class but outside all the methods and blocks is an instance variable. The general scope of an instance variable is throughout the class except in static methods. The lifetime of an instance variable is until the object stays in memory. ExampleIn the example below, we define an instance variable puppyAge in the Puppy class, and using its setAge(), we're modifying it, and using the getAge() method, we're getting it. This variable is available till the lifetime of myPuppy object instance. package com.tutorialspoint;
public class Puppy {
private int puppyAge;
public void setAge( int age ) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge( ) {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge( 2 );
System.out.println("Puppy Age :" + myPuppy.getAge() );
}
}
Compile and run the program. This will produce the following result − Output Puppy Age :2 Scope of Java Class (Static) VariablesA variable that is declared inside a class, outside all the blocks, and is marked static is known as a class variable. The general scope of a class variable is throughout the class, and the lifetime of a class variable is until the end of the program or as long as the class is loaded in memory. ExampleIn the example below, we define a class variable BREED in Puppy class. This variable is available till the lifetime of program. Being static in nature, we can access it using class name directly as shown below: package com.tutorialspoint;
public class Puppy {
private int puppyAge;
public static String BREED="Bulldog";
public void setAge( int age ) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge( ) {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge( 2 );
System.out.println("Puppy Age :" + myPuppy.getAge() );
// access the class variable
System.out.println("Breed :" + Puppy.BREED );
}
}
Compile and run the program. This will produce the following result − Output Puppy Age :2 Breed :Bulldog Scope of Java Local VariablesAll other variables that are not instance and class variables are treated as local variables, including the parameters in a method. The scope of a local variable is within the block in which it is declared, and the lifetime of a local variable is until the control leaves the block in which it is declared. ExampleIn the example below, we define two local variables in the main() method of the Puppy class. These variables are available till the lifetime of the method or block in which they are declared and can be accessed as shown below: package com.tutorialspoint;
public class Puppy {
private int puppyAge;
public static String BREED="Bulldog";
public void setAge( int age ) {
// access the instance variable and modify it
puppyAge = age;
}
public int getAge( ) {
// access the instance variable
return puppyAge;
}
public static void main(String []args) {
Puppy myPuppy = new Puppy();
myPuppy.setAge( 2 );
System.out.println("Puppy Age :" + myPuppy.getAge() );
// access the class variable
System.out.println("Breed :" + Puppy.BREED );
// local variables
int a = 10;
int b = 20;
int c = a + b;
System.out.println("c: " + c);
}
}
Compile and run the program. This will produce the following result − Output Puppy Age :2 Breed :Bulldog c: 30 Important Points About Variables Scope
Advertisements
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |