Hi Guest

MobileUser

1 HomePage 1 Jobs 1 WalkIn 1Articles  

Cat Exam

 :) Latest Topic
general knowledge
VB.NET
SQL Query
UDDI
CLASS
JavaScript Questions
SharePoint Interview
SilverLight
WCF
beans
 ASP.NET
 :) Hot Jobs
 
 :) Latest Articles


  

 


Can you define variable shadowing in Java with example?
Question Posted on 08 Aug 2021

Home >> Java >> Java Questions >> Can you define variable shadowing in Java with example?




Can you define variable shadowing in Java with example?
If any of instance variable and the local variables have the common same name. And when we access this variable then it will print the local variable which will shadowing the instance variable. Below are the some reasons for considering a variable shadowing.
(i)When method is overridden in the child class.
(ii)When subclass declares a variable with same name of the parent class variable.
(iii)There are some situation where a variable in local scope with same name as the name of a variable defined in and instance scope.
Below is the example for variable shadowing

public class testShadowing
{
 string name="interviewqsn";
 int subject=12;
 public void displaylocal()
 {
  string name="crackyourinterview";
  int subject=20;
  System.out.println("Name:-" + name);
  System.out.println("subject:-" + subject);
 }
 public static void main(String args[])
 {
  new testShadowing().displaylocal();
 }
}


Output:-
Name:- crackyourinterview
subject:- 20
0
0



 
 Input Your Comments Or suggestions(If Any)
 Name
 Email(optional)
 Comments


Other Important Questions
What are the main difference between Array and Arraylist
What is classloader in Java and name of some classloader
Different modules of the Spring framework
Difference between Object and Class
Most important 15 aspects that Java Developer should learn in year 2020






 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |