• Post Reply Bookmark Topic Watch Topic
  • New Topic

Not a statement error + JVM question  RSS feed

 
Larry Olson
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Question 1:

I am getting not a statement error on the following java code sample (assume this is within a class and in the main method)

if(true)
Object o = new Object(); //# 1

But the error goes away if I do:

if(true)
{
Object o = new Object(); // # 2
}

My interpretation of why the compiler throws the error at #1 is that the scope of the variable is ambiguous in that context and wide open to interpretation. So the compiler cannot decide the correct scope. But in # 2, by enclosing the statement within curly braces, the scope becomes clear i.e. the scope is local within the block. Hence the compiler is happy.

Is my interpretation correct? Kindly provide your views.

==============================================================================================================

Question 2:

This question is about the JVM. The question is "When does a JVM run on my machine"?

Is it when I open a command window and type "java MyClass"" ? i.e when I run a java program? Does it mean if I open ten command windows and run "java MyClass" on each one of them, then am I running ten different JVMs on my machine?

If this is true, is each JVM independent of each other i.e. they aren't aware of each other and they have their own memory space / heap etc. etc. i.e. each JVM lives in its own world not being aware of the other JVMs whatsoever?

Thanks for your help in clarifying these questions.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1) I think it is for scoping. A variable is defined in the scope of a set of braces. Since there are no braces, what would it mean?
2) Yes. 10 JVMs are created and they are independent.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!