• Post Reply Bookmark Topic Watch Topic
  • New Topic

variable scoping  RSS feed

 
Lucky J Verma
Ranch Hand
Posts: 278
  • Mark post as helpful
  • send pies
  • 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.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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.
 
Remko Strating
Ranch Hand
Posts: 893
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • 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?
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
are you sure you don't have an old .class file hanging around?
 
Mohammad Hossain
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
  • 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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!