local-variables

Why it says that “Cannot refer to a non-final variable i inside an inner class defined in a different method”? [duplicate]

北慕城南 提交于 2019-11-27 01:23:05
问题 This question already has an answer here: Cannot refer to a non-final variable inside an inner class defined in a different method 20 answers I have button click listener and in onCreate() method I have a local variable like onCreate() { super.onCreate(); int i = 10; Button button = (Button)findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { i++; } }); Why java asks for to make me final ? 回答1: When the onCreate() method

Dynamically set local variables in Ruby [duplicate]

这一生的挚爱 提交于 2019-11-26 23:00:55
问题 This question already has an answer here: How to dynamically create a local variable? 4 answers I'm interested in dynamically setting local variables in Ruby. Not creating methods, constants, or instance variables. So something like: args[:a] = 1 args.each_pair do |k,v| Object.make_instance_var k,v end puts a > 1 I want locally variables specifically because the method in question lives in a model and I dont want to pollute the global or object space. 回答1: The problem here is that the block

Where are .NET local variables stored?

走远了吗. 提交于 2019-11-26 21:33:25
问题 In IL, you can define local variables using the .locals directive. Where are these variables stored, stack or heap? 回答1: It is very much an implementation detail of the JIT compiler. It will try very hard to store local variables in a CPU register, very efficient. The stack is the usual backing store, in case there aren't enough registers available to store all the local variables. Big difference between the x86 and x64 jitters for example. x64 has a lot more registers available. This also

Default Values and Initialization in Java

核能气质少年 提交于 2019-11-26 17:04:24
Based on my reference , primitive types have default values and Objects are null. I tested a piece of code. public class Main { public static void main(String[] args) { int a; System.out.println(a); } } The line System.out.println(a); will be an error pointing at the variable a that says variable a might not have been initialized whereas in the given reference, integer will have 0 as a default value. However, with the given code below, it will actually print 0 . public class Main { static int a; public static void main(String[] args) { System.out.println(a); } } What could possibly go wrong

Python local vs global variables

陌路散爱 提交于 2019-11-26 14:36:35
问题 I understand the concept of local and global variables in Python, but I just have a question about why the error comes out the way it is in the following code. Python execute the codes line by line, so it does not know that a is a local variable until it reads line 5. Does Python go back one line and tag it as an error after it tries to execute line 5? a=0 def test(): print a #line 4, Error : local variable 'a' referenced before assignment a=0 #line 5 test() 回答1: Setup and Testing To analyze

Returning string from C function

吃可爱长大的小学妹 提交于 2019-11-26 14:21:35
I haven't used C in over 3 years, I'm pretty rusty on a lot of things. I know this may seem stupid but I cannot return a string from a function at the moment. Please assume that: I cannot use string.h for this. Here is my code: #include <ncurses.h> char * getStr(int length) { char word[length]; for (int i = 0; i < length; i++) { word[i] = getch(); } word[i] = '\0'; return word; } int main() { char wordd[10]; initscr(); *wordd = getStr(10); printw("The string is:\n"); printw("%s\n",*wordd); getch(); endwin(); return 0; } I can capture the string (with my getStr function) but I cannot get it to

C++ local variable destruction order

﹥>﹥吖頭↗ 提交于 2019-11-26 13:48:13
问题 Is there a defined order in which local variables are deallocated in C++ (11) ? To be more concise: In which order will side effects of the destructors of two local variables in the same scope become visible? e.g.: struct X{ ~X(){/*do something*/} } int main(){ X x1; X x2; return 0; } Is x1 or x2 destroyed first when main returns or is the order undefined in C++11? 回答1: Within each category of storage classes (except dynamically allocated objects), objects are destructed in the reverse order

Access a function variable outside the function without using “global”

馋奶兔 提交于 2019-11-26 11:44:39
I am trying to access a local function variable outside the function in Python. So, for example, bye = '' def hi(): global bye something something bye = 5 sigh = 10 hi() print bye The above works fine as it should. Since I want to find out if I can access bye outside hi() without using global bye , I tried: def hi(): something something bye = 5 sigh = 10 return hi() x = hi() print x.bye The above gives AttributeError: 'NoneType' object has no attribute 'bye' . Then, I tried: def hi(): something something bye = 5 sigh = 10 return bye hi() x = hi() print x.bye This time it doesn't give even an

Inner class and local variables

早过忘川 提交于 2019-11-26 11:29:34
问题 Why do I need to declare a local variable as final if my Inner class defined within the method needs to use it ? Example : class MyOuter2 { private String x = \"Outer2\"; void doStuff() { final String y = \"Hello World\"; final class MyInner { String z = y; public void seeOuter() { System.out.println(\"Outer x is \"+x); System.out.println(\"Local variable is \"+y); MyInner mi = new MyInner(); mi.seeOuter(); } } } } Why the String y needs to be a final constant ? How does it impact ? 回答1: The

Python global/local variables

為{幸葍}努か 提交于 2019-11-26 11:18:28
问题 Why does this code work: var = 0 def func(num): print num var = 1 if num != 0: func(num-1) func(10) but this one gives a \"local variable \'var\' referenced before assignment\" error: var = 0 def func(num): print num var = var if num != 0: func(num-1) func(10) 回答1: Because in the first code, you have created a local variable var and used its value, whereas in the 2nd code, you are using the local variable var , without defining it. So, if you want to make your 2nd function work, you need to