A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/java/java_variable_scope.htm below:

Java - Variable Scopes

Java - Variable Scopes

The 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 Variables

A 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.

Example

In 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) Variables

A 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.

Example

In 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 Variables

All 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.

Example

In 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

RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4