• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Not a statement error + JVM question

 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author & internet detective
Posts: 41860
908
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I found some pretty shells, some sea glass and this lovely tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic