Questions tagged [shadowing]

In computer programming, shadowing occurs when a variable declared within a certain scope (decision block, method or inner class) has the same name as a variable declared in an outer scope. This can lead to confusion, as it may be unclear which variable subsequent uses of the shadowed variable name refer to, which depends on the name resolution rules of the language.

One of the first languages to introduce variable shadowing was ALGOL, which first introduced blocks to establish scopes. It was also permitted by many of the derivative programming languages including C++ and Java.

The C# language breaks this tradition, allowing variable shadowing between an inner and an outer class, and between a method and its containing class, but not between an if-block and its containing method, or between case statements in a switch block.

237 questions
0
votes
2 answers

SyntaxError when adding to a global set inside a function (Python)

I am trying to write a function that reads keywords from a file (in the format that each keyword is on a new line inside the text file) I want the function to put the keywords into a global set() called "desiredItems". desiredItems = set() def…
Benjels
  • 1
  • 1
0
votes
2 answers

How to use shadowing with global variables?

I have notes from class but I am unsure what is actually happening. Other than adding to confusion, what purpose does shadowing allow on to do? I thought the because globalString is a string type it cannot be modified? How do I access the original…
Nick Lee
  • 842
  • 2
  • 11
  • 27
0
votes
1 answer

Using base model class(es) without modifying it in asp.net mvc

Let's say I created a few models via Entity Framework, and one of them is called Paper_Results. This is how the class might look: public partial class Paper_Results { public string Color { get; set; } public int Height { get; set; } public…
Andy Narain
  • 55
  • 2
  • 8
0
votes
0 answers

Possible angular scope shadowing unexplained

I have tried several different methods or wrapping a simple jquery-ui slider, the first of which is: //Slider control TDApp.directive("tdControlSlider", ["TDSV", function (TDSV) { return { scope: { property: "=", …
AaronF
  • 2,841
  • 3
  • 22
  • 32
0
votes
3 answers

How to use a global variable within a function in Haskell

In section Incorrectly matching against a variable from chapter 3 of real world haskell, there is an example as follows: -- file: ch03/BogusPattern.hs data Fruit = Apple | Orange apple = "apple" orange = "orange" whichFruit :: String ->…
user811416
  • 205
  • 1
  • 2
  • 11
0
votes
2 answers

Shadowing instance variables with local variables in Java

I have read that " a variable is shadowed if there is another variable with the same name that is closer in scope". I found this Point class with a constructor as an example: public class Point { public int x = 0; public int y = 0; …
user3370587
0
votes
1 answer

Unable to reset global timer from javascript function

I've tried various ways to make sure that my timer variable is global (and I believe it is) but why can't I clear the interval? var timer; function refreshtimer(timer) { stoptimer(timer); timer = window.setInterval(postmsg,…
Shardj
  • 1,800
  • 2
  • 17
  • 43
0
votes
1 answer

Python: unexplainable case in variable copying

Given the following program, I want the variable bar to keep the same. def foo(bar): bar2 = bar[:] chg = [] for p in range(4): for q in range(3): chg.append([p,q]) for [x,y] in chg: bar2[x][y] = "xx" …
Lewistrick
  • 2,649
  • 6
  • 31
  • 42
0
votes
1 answer

Field shadowed by local variable from the point of view of an anonymous class

I'm trying to run this code: class A { int x = 123; public void f(int x) { new Runnable() { public void run() { System.out.println(x); } }.run(); } static { A a = new A(); a.f(33); } } But it's giving…
Dog
  • 7,707
  • 8
  • 40
  • 74
0
votes
1 answer

How to handle packages shadowing each other?

I've installed most of my modules through Ubuntu packages, and consequently they're in /usr/lib/python3/dist-packages. Some are too old or not available through Ubuntu, so I've installed them through pip, and they're in…
gerrit
  • 24,025
  • 17
  • 97
  • 170
0
votes
1 answer

Field shadowing or overriding?

I have two classes (A and B) and B extends A. public class A { protected int i = 1; } public class B extends A{ protected int i = 2; } In this case the program writes 1. A a = new B(); System.out.println(a.i); //1 But if I assign value i in…
user2693979
  • 2,482
  • 4
  • 24
  • 23
0
votes
1 answer

Field Name Best Practices (Shadowing or Compund Names)

As the red block above (warning that this is a subjective question and may be closed) there may not be a stone etched law on this, but I don't see why that would warrant closing a question. ...Rant aside I am planning on implementing Hibernate as my…
LostNomad311
  • 1,975
  • 2
  • 23
  • 31
0
votes
1 answer

Action Listener for JButton isn't working

I'm trying to learn java by myself, and am looking to make a secure text editor that you have to log into to access the text. However, the action listener isn't working for any of the buttons, and i can't figure out what's wrong. Please note I've…
gbsr64
  • 13
  • 3
0
votes
1 answer

ActionPerformed does not work

Have a little problem with this code. The actionPerformed method doesn't work. Buttons knappStartSalg and knappStartKunde, don't react when I push the buttons. All that should have been imported are imported. Will be very thankful for any…
mv700
  • 35
  • 1
  • 8
0
votes
1 answer

Java inherited field shadowing and the JVM?

Is the mechanism how fields are shadowed/hidden by inheritance and later resolved part of the JVM spec? I know it is part of the Java spec, and can be found in many blog posts and SO questions. However, when I actually look at the JVM spec for field…
Li Haoyi
  • 15,330
  • 17
  • 80
  • 137
1 2 3
15
16