When a local variable has the same name as a field the local variable does this to the fields name?

Local variables should not have the same name as class fields

Overriding or shadowing a variable declared in an outer scope can strongly impact the readability, and therefore the maintainability, of a piece of code. Further, it could lead maintainers to introduce bugs because they think they’re using one variable but are really using another.

Noncompliant Code Example

class Foo {
  public $myField;

  public function doSomething() {
    $myField = 0;
    ...
  }
}

Deprecated

This rule is deprecated, and will eventually be removed.

Variable in Java is a data container that saves the data values during Java program execution. Every variable is assigned a data type that designates the type and quantity of value it can hold. A variable is a memory location name for the data.

A variable is a name given to a memory location. It is the basic unit of storage in a program.

  • The value stored in a variable can be changed during program execution.
  • A variable is only a name given to a memory location. All the operations done on the variable affect that memory location.
  • In Java, all variables must be declared before use.

How to declare variables?

We can declare variables in Java as pictorially depicted below as a visual aid.

When a local variable has the same name as a field the local variable does this to the fields name?

From the image, it can be easily perceived that while declaring a variable, we need to take care of two things that are:

1. datatype: Type of data that can be stored in this variable. 

2. data_name: Name given to the variable. 

In this way, a name can only be given to a memory location. It can be assigned values in two ways: 

  • Variable Initialization
  • Assigning value by taking input

How to initialize variables?

It can be perceived with the help of 3 components that are as follows:

  • datatype: Type of data that can be stored in this variable.
  • variable_name: Name given to the variable.
  • value: It is the initial value stored in the variable.

When a local variable has the same name as a field the local variable does this to the fields name?

Illustrations: 

float simpleInterest; 
// Declaring float variable
int time = 10, speed = 20; 
// Declaring and initializing integer variable
char var = 'h'; 
// Declaring and initializing character variable

Types of Variables in Java

Now let us discuss different types of variables which are listed asfollows: 

  1. Local Variables
  2. Instance Variables
  3. Static Variables

When a local variable has the same name as a field the local variable does this to the fields name?

Let us discuss the traits of every type of variable listed here in detail.

1. Local Variables 

A variable defined within a block or method or constructor is called a local variable. 

  • These variables are created when the block is entered, or the function is called and destroyed after exiting from the block or when the call returns from the function.
  • The scope of these variables exists only within the block in which the variables are declared, i.e., we can access these variables only within that block.
  • Initialization of the local variable is mandatory before using it in the defined scope.

Java

import java.io.*;

class GFG {

    public static void main(String[] args)

    {

        int var = 10;

        System.out.println("Local Variable: " + var);

    }

}

2. Instance Variables

Instance variables are non-static variables and are declared in a class outside of any method, constructor, or block. 

  • As instance variables are declared in a class, these variables are created when an object of the class is created and destroyed when the object is destroyed.
  • Unlike local variables, we may use access specifiers for instance variables. If we do not specify any access specifier, then the default access specifier will be used.
  • Initialization of an instance variable is not mandatory. Its default value is 0.
  • Instance variables can be accessed only by creating objects.

Java

import java.io.*;

class GFG {

    public String geek;

    public GFG()

    {

        this.geek = "Shubham Jain";

    }

    public static void main(String[] args)

    {

        GFG name = new GFG();

        System.out.println("Geek name is: " + name.geek);

    }

}

Output

Geek name is: Shubham Jain

3. Static Variables

Static variables are also known as class variables. 

  • These variables are declared similarly as instance variables. The difference is that static variables are declared using the static keyword within a class outside of any method, constructor or block.
  • Unlike instance variables, we can only have one copy of a static variable per class, irrespective of how many objects we create.
  • Static variables are created at the start of program execution and destroyed automatically when execution ends.
  • Initialization of a static variable is not mandatory. Its default value is 0.
  • If we access a static variable like an instance variable (through an object), the compiler will show a warning message, which won’t halt the program. The compiler will replace the object name with the class name automatically.
  • If we access a static variable without the class name, the compiler will automatically append the class name.

Java

import java.io.*;

class GFG {

  public static String geek = "Shubham Jain";        

    public static void main (String[] args) {

        System.out.println("Geek Name is : "+GFG.geek);

    }

}

Output

Geek Name is : Shubham Jain

Differences between the Instance variables and the Static variables

Now let us discuss the differences between the Instance variables and the Static variables:

  • Each object will have its own copy of an instance variable, whereas we can only have one copy of a static variable per class, irrespective of how many objects we create.
  • Changes made in an instance variable using one object will not be reflected in other objects as each object has its own copy of the instance variable. In the case of a static variable, changes will be reflected in other objects as static variables are common to all objects of a class.
  • We can access instance variables through object references, and static variables can be accessed directly using the class name.

Syntax: Static and instance variables

class GFG
{
    // Static variable
    static int a; 
    
    // Instance variable
    int b;        
} 

Must Read: 

  • Rules of Variable Declaration in Java
  • Scope of Variables in Java
  • Comparison of static keyword in C++ and Java
  • Are static local variables allowed in Java?
  • Instance Variable Hiding in Java

This article is contributed by Harsh Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to . See your article appearing on the GeeksforGeeks main page and help other Geeks. Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


When a local variable in an instance method has the same name as an instance field the instance field hides the local variable?

A method may have a local variable with the same name as an instance field. This is called shadowing. The local variable will hide the value of the instance field.
You cannot declare two variables with the same name in the same scope.

Can a variable with the same type and name be declared inside two different methods?

In a word, yes. Variable names only hold in the scope they're defined in, and you can use the same name in different scopes.

What kind of variable do you use if you need to share a variable from one instance of a class to the next?

You need to make the variables in class aaa as class variables, and then you can use these variables of class aaa in class bbb by using object of class aaa. e.g. aaa obj2=new aaa(); System.