• Post Reply Bookmark Topic Watch Topic
  • New Topic

int variables and printf in Java  RSS feed

 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I write the following statement:



and I get an error!

The wierdest thing is that 'num1' does NOT show in variables window. How can it be?

 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't know what this "variables window" is, but in general when posting questions on a forum it helps if you provide useful detail about the question. Vague phrases like "I get an error" are less than helpful. So have a look at our FAQ pages HowToAskQuestionsOnJavaRanch for a lot of information about how to ask, and TellTheDetails for more specific information.
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK.

The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
num1 cannot be resolved to a variable

If I delete the line: "System.out.printf("%d", num1);", the program compiles and runs.
However, my variable window doesn't seem to include num1.
Print Screen of "variable window" is attached
Screenshot.jpg
[Thumbnail for Screenshot.jpg]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matan Bar Lev wrote:OK.

The error is:
Exception in thread "main" java.lang.Error: Unresolved compilation problem:
num1 cannot be resolved to a variable

If I delete the line: "System.out.printf("%d", num1);", the program compiles and runs.
However, my variable window doesn't seem to include num1.
Print Screen of "variable window" is attached



You are going to have to show us more than two lines of code, if you want any help.


And also, read this ... http://www.coderanch.com/how-to/java/FixAllCompilerErrorsBeforeRunningTheApplication

Henry
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But that variable window is what you use in Eclipse for debugging code, when you run it. You shouldn't be running code until it compiles successfully -- which is what the error message is telling you, in part.

The other part of the message is that you haven't declared the "num1" variable. You'll have to look at the source code to figure out why not.
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is the whole code:



I run in debugging mode after excluding the line "System.out.printf("%d", num1);". In this case, the program compiles and runs.
I put a breakpoint at the last line and still I can't see 'num1', 'num2' and 'temp' in variables window.
The strange part is the 'num3' DOES show in variables window (See attached image).
Screenshot.jpg
[Thumbnail for Screenshot.jpg]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Local variables are only in scope from the point that it is declared -- and for the block that it is declared in. It is *not* in scope outside of the block that it is declared in -- as you have shown in your example.

Henry
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean by saying "block"?

Why does 'num3' is inside block, while 'num1' and 'num2' are outside block?


 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matan Bar Lev wrote:What do you mean by saying "block"?

Why does 'num3' is inside block, while 'num1' and 'num2' are outside block?


Basically, based on how you declared the variables. The num1 variable is only in scope from line 16 to line 38 (from your code above). The num2 variable is only in scope from line 42 to line 49. And the num3 variable is only in scope from line 52 to line 56.... and of course, you are not allowed to use any variable that is not in scope.

Henry
 
Matan Bar Lev
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a lot.

This also explains why there was an error in "Systet.out.printf("%d", num1)".... 'num1' was out of block.

Probably, the lesson is that it's recommended to declare all variables at the beginning of the method (just like in C).

 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matan Bar Lev wrote:
Probably, the lesson is that it's recommended to declare all variables at the beginning of the method (just like in C).



Actually, that is not true anymore. Most compilers these days do follow the standard that was released in 1999, and allows local variables to be declared anywhere in the function.

Unfortunately, the one big exception to this are the Microsoft compilers (which still follows the older standard). This means that when I code in C, I generally always put all local variables at the top of the function -- knowing that windows will always eventually be one of the target platforms.

Henry
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Matan Bar Lev wrote:Probably, the lesson is that it's recommended to declare all variables at the beginning of the method (just like in C).


No, the actual rule is to declare variables in the smallest possible block. You chose a block which was too small, and you should declare num1 outside that block, but inside the smallest block which also contains the line of code where you use the variable.

As for "just like in C" -- that's a concept which you should try to get rid of. Java is not just C with a slightly different syntax, and using C concepts to help you understand Java concepts is at best going to result in you writing Java with a C accent. And at worst going to lead to code which doesn't work.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!