• 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

variable scoping

 
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
HI,
I am somewhat confused in variable scoping rules.


I wrote a program ,with method..
class MM{
void method1(){
Object o; //not initializing
System.out.println(o); //just printing
}
public static void main(String...args){
MM mm=new MM();
//simple object
//no call to method1

}}


I have created a buggy method ,with no-object initialization but using it.although method is not called.
& I wonder ,program is running ,when it should not be.
As nonreachable code is error in java.its not shoring compilation error.

why is this so.
thank you.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is your question, exactly? This code won't compile, because "o" is not initialized before use. All local variables must be definitely assigned before they are read.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code is not running and gives a variable might not initialized error.



Maybe you've an old class file which you run.
Try to delete that one.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My question is code shouldnt have been compile as o is not initialized but it compiles & runs.
why is this so.
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi remko ,

I tried , its runnnig .. i made changes ....like putting println statement ...these changes are showing up,

public class VariablesDemo1 {

void methodVerify()
{
System.out.println("method call");
Object oo;
System.out.println("object"+oo);
}

public static void main(String args[])
{
System.out.println("method call");
VariablesDemo1 vd=new VariablesDemo1();
vd.methodVerify();
}}
 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried this program on command line,here its shows error
but in eclipse,it was running uptil use of uninitialized object.
 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's really compiling for you, then what is the 'System.out.println("object"+oo);' line printing when you run it?

Seriously, a standard Java compiler will not compile this. What are you using to compile it with? Perhaps you're using some sort of hobby compiler which accepts non-standard Java code?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
are you sure you don't have an old .class file hanging around?
 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It looks like that you have not initialized Object O. So, it gives a compiler error. Are you using javac and java command or some IDE.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic